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.
Related
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.
I want to pass comma separated string to IN clause. My controller code is as:-
$batchpost = $this->input->post('batch');//print_r($batch);
$batch_id = '';
for($i=0;$i<count($batchpost);$i++ ) {
$batch_id = $batch_id . $batchpost[$i] . ',';
}
$batch_string = rtrim($batch_id, ',');
$batch = str_replace(",", ",", $batch_string) ;
$list = $this->admin_model->get_attendance_datatables($batch);
Here $batchpost will return array value, and I want to convert it to a comma separated string and pass to the model.
My model code is as:-
$this->db->select('offline_student.*,batch.batch_name,offline_course.course');
$this->db->from($this->table8);
$this->db->join('offline_course', 'offline_course.id = offline_student.course', 'left');
$this->db->join('batch', 'batch.id = offline_student.batch', 'left');
$this->db->where('offline_student.status', 'Active');
$this->db->where_in('batch.id', $batch);
$this->db->order_by('offline_student.id', 'asc');
Suppose in a row for batch column having two value(2,3)in database, if I pass only '2' or '2,3' to model, query will return my result, but when i pass only 3, It's not showing that record.
Remove the following lines:
$batch_id = '';
for($i=0;$i<count($batchpost);$i++ ) {
$batch_id = $batch_id . $batchpost[$i] . ',';
}
$batch_string = rtrim($batch_id, ',');
$batch = str_replace(",", ",", $batch_string) ;
...and make it like this:
$batchpost = $this->input->post('batch');//print_r($batch);
$list = $this->admin_model->get_attendance_datatables($batchpost);
you don't need to convert $batchpost to comma separated string, because where_in() method expects an array on it's second param.
See the docs of where_in(), here https://www.codeigniter.com/userguide3/database/query_builder.html#looking-for-specific-data
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.
The following code is supposed to check for the column names of a table. Then check to see if a corresponding variable has been $_POST and if it has add it to the $SQL. I believe there is a problem with the array that contains a series of arrays but I don't know how to dix it.
$where = $_POST['where'];
$is = $_POST['is'];
$table = $_POST['table'];
$sql = "UPDATE $table SET";
$array = array();
$columnnames = columnnames('blog');
foreach ($columnnames as $columnname){
if($_POST[$columnname]){
$sql .= " $columnname = :$columnname,";
$array .= array(':$columnname' => $_POST[$columnname],);
}
}
$sql = rtrim($sql,',');
$array = rtrim($array,',');
$sql .= " WHERE $where = '$is'";
$q = $rikdb->prepare($sql);
$q->execute($array);
For the sake of comprehension please except that $columnnames = columnnames('blog'); works as it does.
Change this:
$array .= array(':$columnname' => $_POST[$columnname],);
to this:
$array[':$columnname'] = $_POST[$columnname];
and after that use rtrim only on $sql.
the problem is here $array .= array(':$columnname' => $_POST[$columnname],);
you have a , part of the value which is not literal. change it to be like
$array .= array(':$columnname' => $_POST[$columnname] . ",");
You can also add your column names and values to an array and implode(",", $array) so you don't have to use rtrim
Instead of using
$array .= array(':$columnname' => $_POST[$columnname],);
and applying rtrim on results, I'd suggest the easier and failsafe method:
$array[':$columnname'] = $_POST[$columnname];
This is similar to one of my previous questions here, although this is looking for a solution rather than tools to debug it. I'm trying to build a script to (amongst other things) automatically replace names entered in a MySQL database with the name bolded in WikiMedia format, so when I enter NAME: I expect to get '''NAME''':. What I actually get is NAME'''''':. I've tried a sugestion posted on the aforementioned question to remove Carriage Returns from my array using str_replace(array("\r", "\n"), array('', ''), $row); but it had no effect.
The code I'm using to generate this is:
<?php
$link = mysql_connect($host, $username, $pass);
$db = mysql_select_db($database, $link);
$query = "SELECT name FROM " . $prefix . "";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$name[] = $row['name'];
}
$sim = $_POST['sim']; //Fetches SIM from text box.
$sim_wrap = wordwrap($sim, 80, "\n"); //Constrains to 80 columns for readability.
$sim_penultimate = str_replace("::", "<nowiki>::</nowiki>", $sim_wrap);
$sim_final = str_replace($row . ":", "'''" . $row . "''':", $sim_penultimate); //Bold names
echo stripslashes($sim_final); //Removes slashes wordwrap() adds on some configurations example (James\'s).
?>
Thank you for any help you can give me, this really has me stumped.
$row is a result from mysql_fetch_array() - in other words, it's an array. Why are you concatenating it with a string ($row . ":")? Concatenating an array with a string doesn't work - you instead need to concatenate individual elements.
It seems like what you really want to do is something like this...
$names = array();
$replaces = array();
while($row = mysql_fetch_array($result)){
$names[] = $row['name'] . ":";
$replaces[] = "'''" . $row['name'] . "''':";
}
and then later on...
$sim_final = str_replace($names, $replaces, $sim_penultimate); // Bold names