3

Is there a more elegant way to implode data to be queried?

 3 years ago
source link: https://www.codesd.com/item/is-there-a-more-elegant-way-to-implode-data-to-be-queried.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Is there a more elegant way to implode data to be queried?

advertisements

I am going to ask because of this answer.

My code looks like

<?php
$lines = file('file.txt');
$count = count($lines);
$i = 0;
$query = "INSERT INTO table VALUES ";
foreach($lines as $line){
    $i++;
    if ($count == $i) {
        $query .= "('".$line."')";
    }
    else{
        $query .= "('".$line."'),";
    }
}
echo $query;

is there more elegant way to do this/function in php?


foreach ( $lines AS $line )
{
  $query[] = "($line)";
}  

echo  "INSERT INTO table VALUES " . implode(",",$query);

is how to do it with implode but i think AlienWebguy's is better

Related Articles

Is there a more elegant way to do these operations bit by bit?

I built this program that does some bitwise operations on three numbers: 2, 4 and 20: public static void main(String[] args) { int mask = 63; int id = 2; id = (id << 6) | 4; id = (id << 6) | 20; int v3 = id & mask; int v2 = (id >> 6)

Is there a more elegant way to act on the first and last articles in a foreach enumeration than count ++?

Is there a more elegant way to act on the first and last items when iterating through a foreach loop than incrementing a separate counter and checking it each time? For instance, the following code outputs: >>> [line1], [line2], [line3], [line4]

Is there a more elegant way to do this in .NET 4.0?

It appears that C# 4.0 does not support covariance (using the "in" keyword) on parameters in overrides; is that the case? If so, is there a more elegant way to do this? CONTEXT public interface IBaseEvent { /* ... */ } public interface IDerivedE

Is there a more elegant way to find unique NSDictionary keys in an NSArray?

Aim: To obtain an NSArray containing unique keys for given NSDictionary(s) using elegant code Example Code with Current Working Solution: NSArray *data = [[NSArray alloc] initWithObjects: [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWit

Is there a more elegant way to remember user input after submission for drop-down lists in an HTML form?

I have a number field and a drop-down list that I am using as part of a form, as follows: <label for="height">Height/Thickness</label>: <input <?php if (empty($messages) == false) {echo 'value="'.htmlentities($_POST['heigh

Is there a more elegant way to overwrite A with B only if B is greater than A?

have some code in which I often do something like the following: if(B > A) A = B; And I was just wondering if there is a more elegant way to do this (using a minimal amount of variable references) as I am working with big nasty nested arrays and thus

Is there a more elegant way to formulate this Python loop?

my question is related to Python loops in general. I've created a loop that iterates through each item in an XML object. It then appends the retrieved value to a result list. Is there a more elegant or compact way to write this? Best practices? i = 0

Is there a more elegant way to add an article to a Dictionary & lt; & gt; without issue?

I need to add key/object pairs to a dictionary, but I of course need to first check if the key already exists otherwise I get a "key already exists in dictionary" error. The code below solves this but is clunky. What is a more elegant way of doi

Is there a more elegant way to convert the two-digit four-digit years with lubridate?

If a date vector has two-digit years, mdy() turns years between 00 and 68 into 21st Century years and years between 69 and 99 into 20th Century years. For example: library(lubridate) mdy(c("1/2/54","1/2/68","1/2/69","1/2

Is there a more elegant way to add usercontrols to a list?

I have 10 custom usercontrol's with ID's usercontrol1, usercontrol2, usercontrol3......usercontrol10. I want to add all these usercontrols to a List "allusercontrol". I do this with brute force like this allusercontrol.Add(usercontrol1); alluser

Is there a more elegant way to filter dictations in python

This question already has an answer here: Sort a Python dictionary by value 38 answers I am wondering is it possible to filter a dict more shortly than >>> a={1:'a',2:'b',3:'c',4:'d',5:'e'} >>> filterlist=[1,3,5] >>> b=dict((key

Is there a more elegant way to apply css to & ldquo; all elements, but this one?

I have some html/jQuery that I am using to implement some tabbed navigation. When a new tab is selected I need to make it's css class "active" and change the previous tabs css class to "inactive". To do this I have been using the follo

Is there a more elegant way to nest tables in C #?

I am translating a matrix from a C++ project that looks like this: static int grad4[][4] = {{0,1,1,1}, {0,1,1,-1}, {0,1,-1,1}, {0,1,-1,-1}, {-1,1,1,0}, {-1,1,-1,0}, {-1,-1,1,0}, {-1,-1,-1,0}}; and basically the way to do it in C# is nesting arrays, r

Is there a more elegant way of doing serialized Javascript by hand in .Net?

I receive this error when I try to use "{" to generate Javascript objects server-side to pass back to the browser: "Input string was not in a correct format." This happens when I do the following: builder.AppendLine(String.Format("

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK