this is my code :
<?php
$sss = "SELECT tanggal_awal FROM libur_nasional GROUP BY tanggal_awal ASC";
$qqq = mysqli_query($konek,$sss);
$arr = array();
while ( $tam = mysqli_fetch_array($qqq)) {
$date = $tam['tanggal_awal'];
$reformat_date = date("n-j-Y", strtotime($date));
$arr[] = $reformat_date;
}
$array_date = implode(",", $arr);
?>
> output : 8-9-2018,8-10-2018,8-17-2018
> output i want to is : "8-9-2018", "8-10-2018", "8-17-2018"
could someone help me to get the output i want, thank before.
You can make your code a bit shorter:
$sss = "SELECT tanggal_awal FROM libur_nasional GROUP BY tanggal_awal ASC";
$qqq = mysqli_query($konek,$sss);
while ( $tam = mysqli_fetch_array($qqq)) {
$arr[] = date("n-j-Y", strtotime($tam['tanggal_awal']));
}
$array_date = '"' . implode('", ', $arr) . '"';
In php you don't need to declare an array before pushing values to it.
Your $date variable is not needed as it's just a copy of the $tam['tanggal_awal'] value.
$reformat_date is also not needed as you can place it in the array directly.
The imploding is what is placed between the items, by having ", there you just need to add a " at each end of the string.
$arr=['8-9-2018','8-10-2018','8-17-2018'];
$array_date = '"'.implode('", "', $arr) . '"';
echo $array_date;
Output ---> "8-9-2018", "8-10-2018", "8-17-2018"
Try to implode your array like this.
Related
So the code below gets a name from a db(which is a string) and it outputs with "" and a comma.Well it was suppose to, but for some reason it only adds the quotation marks and not the commas.I need it to add the commas too.Thank for you help.
<?php
$sl = "SELECT _eName FROM vrt;";
$ret = mysqli_query($conn, $sl);
$resultck = mysqli_num_fields($ret);
if ($resultck > 0){
while ($row = mysqli_fetch_assoc(($ret))){
$b = $row['e_Name'];
$temp = array($b,);
$req = "'" . implode ( "' , '", $temp ) . "'";
echo $req;
}}
?>
You're echoing each row, your not building out $temp with all rows then imploding, so it would only contain the one item.
Instead, simply do:
<?php
$conn = ...
$result = mysqli_query($conn, "SELECT e_Name FROM vrt");
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
$data = array_map(fn($row) => $row['e_Name'], $data);
echo "'" . implode ("' , '", $data) . "'";
?>
implode joins array elements with a string. Your variable $temp contains only a single element, so there is nothing to join and the element itself is returned.
hope you can help, tearing my hair out here!
I have...
$result = mysqli_query($con,'SELECT this FROM that');
while($row = mysqli_fetch_array($result))
{
echo $row['this'] . ',';
}
Which returns...
222,225,243,256,260,269,273,280,295,296,
I need to remove the last comma to give me just...
222,225,243,256,260,269,273,280,295,296
I've tried trim rtrim substr and everything else I could find but none of them work. Think it's to do with the concatenation of the $row['this'] . ',' but I cant figure out how to resolve it!
Any help would be appreciated.
Cheers,
Mark.
Just let MySQL do the work for you:
$result = mysqli_query($con, 'SELECT GROUP_CONCAT(this, ',') as thises FROM that');
This constructs the results as a comma-delimited string. You can refer to it by its name, thises.
Use rtrim to remove last comma.
rtrim($my_string,',');
you can use implode
while($row = mysqli_fetch_array($result))
{
$a[]= $row['this'];
}
$b = implode(',',$a);
print_r($b);
function for implode
function imp($c){
$d= implode(',',$c);
return $d;
}
$b = imp($a);
print_r($b);
You can use substr()
This is your code
$result = mysqli_query($con,'SELECT this FROM that');
while($row = mysqli_fetch_array($result))
{
echo $row['this'] . ',';
}
Code Should like this
$result = mysqli_query($con,'SELECT this FROM that');
$my_string = "";
while($row = mysqli_fetch_array($result))
{
$my_string .= $row['this'] . ',';
}
$my_final_string = substr($my_string, 0, strlen($my_string)-1);
echo $my_final_string;
Explanation
substr(string,start,length)
string = Your generated string
start = From which position you want to start the string.
length = A positive number - The length to be returned from the start parameter. Negative number - The length to be returned from the end of the string
i wrote the following code:
<?php
$listO = $_POST["letter"];
//print_r($listO);
//Array ( [0] => A [1] => B [2] => C)
function to_pg_array($set) {
settype($set, 'array'); // can be called with a scalar or array
$result = array();
foreach ($set as $t) {
if (is_array($t)) {
$result[] = to_pg_array($t);
} else {
$t = str_replace('"', '\\"', $t); // escape double quote
if (! is_numeric($t)) // quote only non-numeric values
$t = '"' . $t . '"';
$result[] = $t;
}
}
return '{' . implode(",", $result) . '}'; // format
}
$pg_array_listO = to_pg_array($listO);
//print_r($pg_array_list_organisms);
//{"A","B","C"}
$conn = pg_connect("host=X dbname=Y user=Z");
$result = pg_query_params($conn, 'SELECT count(cp.id)
FROM cp, act, a, t
WHERE t.tid = a.tid AND
a.aid = act.aid AND
act.m = cp.m AND
t.n = $1 AND
act.st = $2 AND
t.o LIKE ANY(ARRAY[$3])', array($t1, $a2, $pg_array_listO));
while($row = pg_fetch_row($result)) {echo $row[0];}
?>
However i can't figure out how to pass the array $pg_array_listO to the postgres query. The function to_pg_array converts the php array into postgres array but still don't work. How can i do this?
postgres array looks like '{list}' :
t=# select array['a','b','c'];
array
---------
{a,b,c}
(1 row)
so you need to get rid of double quotes, otherwise postgres understands literals as identities.
Eg with $pg_array_listO = str_replace('"', '\\"',to_pg_array($listO)) or smth smarter - sorry - I'm not good in php
additionally modify ANY(ARRAY[$3]) to ANY('$3'::text[]), cos array[] or '{}'::text[] would be accepted
update
based on
//print_r($pg_array_list_organisms);
//{"A","B","C"}
I expect this to work:
$result = pg_query_params($conn, "SELECT count(cp.id)
FROM cp, act, a, t
WHERE t.tid = a.tid AND
a.aid = act.aid AND
act.m = cp.m AND
t.n = $1 AND
act.st = $2 AND
t.o LIKE ANY($3)", array($t1, $a2, str_replace('"', '',to_pg_array($listO))));
mind I changed quotes and SQL and str_replace for $3 variable
a working example of this approach:
t=# select 'a' like any('{a,b,c}'::text[]);
?column?
----------
t
(1 row)
I have several variables, for example:
$q1
$q2
$q3
...
$q50
I would like to combine them info one variable who puts them info the VALUE of an INSERT INTO. Maybe a for loop?
INSERT INTO $table_name ($column_names) VALUES($q1, $q2, $q3)
So it might look like this
INSERT INTO $table_name ($column_names) VALUES($combined_variables)
This way, i could just manually add another variable to the $q51 and it would populate VALUE automaticly.
Is this possible? Maybe somehing like this (but this does not work)
$combined_variables = '';
for( $i = 1; $i <= 50 $i++ ) {
$combined_variables .= 'q' . $i . ', ';
}
$combined_variables = substr($combined_variables, 0, -2); //minus 2 to remove the last space and comma
This should work for you:
(Here I start with $q1 and assign it to the array until the next $qX is not set)
<?php
$combined_variables = [];
$count = 1;
while(isset(${"q" . $count})){
$combined_variables[] = ${"q" . $count};
$count++;
}
?>
So as an example:
$q1 = 5;
$q2 = 2;
You would end up with following array:
Array ( [0] => 5 [1] => 2 )
And then you can simply use it in the query like this:
"INSERT INTO $table_name ($column_names) VALUES(" . "'" . implode("','", $combined_variables) . "'" . ")"
You can use variable variables like this:
$combined_variables = array();
for ($i = 1; $i <= 50 $i++) {
$var = 'q' . $i;
$combined_variables[] = ${$var};
}
$combined_variables = implode(', ', $combined_variables);
However, if you can use an array instead of 50 variables you'd have a much easier job.
I am printing a set of words that is placed in a MySQL database and I am retrieving it with PHP. I want to present it as a comma separated list, but I need it not to print or remove the last comma. How could I do this?
I did try to use rtrim, but I did not probably do it right.
This is my code as it is today:
<?php
$query0 = "SELECT LCASE(ord) FROM `keywords` ORDER BY RAND()";
$result0 = mysql_query($query0);
while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC))
{
$keyword = $row0['LCASE(ord)'];
echo "$keyword, ";
?>
I did try to use rtrim, my attempt was something like this (I might be honest enough to say that I am in above my head in this ;) )
$keyword = $row0['LCASE(ord)'];
$keywordc = "$keyword, ";
$keyword- = rtrim($keywordc, ', ');
echo "$keyword-, ";
As you might imagine, this did not print much (but at least it did not leave me with a blank page...)
I would do:
$keywords = array();
while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC))
{
$keywords[] = $row0['LCASE(ord)'];
}
echo implode(',', $keywords);
I usually do this by placing the results in an array first
$some_array = array();
while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC)) {
$some_array[] = $row0['LCASE(ord)'];
}
then simply:
echo "My List: " . implode(', ', $some_array);
// Output looks something like:
My List: ord1, ord2, ord3, ord4
substr($string, 0, -1);
That removes the last character.