I have the folowing query:
$query = $db->query("SELECT name FROM table LIMIT 5");
I want to have a list of these names either with the comma or as a list one above another.
while ($result = $query ->fetch_object()) {
echo '.$result->name.';
}
How can I do that.
Thanks
To have the list with names comma separated try
$list='';
while ($result = $query ->fetch_object()) {
$list.= $result->name.',';
}
echo $list;
here you will get the one trailing comma at end of list, if you want to remove than do
echo $list=substr($list,0,-1);
$arr = [];
while ($result = $query ->fetch_object()) {
$arr[] = $result->name;
}
echo implode(",", $arr);
This will add the results to an array, then join the results on a comma.
If you want a list then you can use
echo "<ul><li>" . implode("</li><li>", $arr) . "</li></ul>;
List:
$query = $db->query("SELECT name FROM table LIMIT 5");
echo "<ul>";
while ($result = $query ->fetch_object()) {
echo "<li>$result->name</li>";
}
echo "</ul>";
Comma:
$query = $db->query("SELECT name FROM table LIMIT 5");
$names = array();
while ($result = $query ->fetch_object()) {
$names[] = $result->name;
}
echo implode(', ', $names);
Dunno what you do with these dots there.
while ($result = $query ->fetch_object()) {
echo $result->name . ', ';
}
If you want it below each other:
while ($result = $query ->fetch_object()) {
echo $result->name . '<br>';
}
Related
I have database query which returns an array of string values. I want them to be comma separated and appended to another string.
$sql1 = "select location.name as name from location,destination where destination.name='".$destination."' AND location.destination_id=destination.destination_id";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
$insert = "";
while($locations = $result1->fetch_assoc()) {
foreach ($locations as $location) {
$insert .= $location['name'] . ",";
}
}
$insert = rtrim($insert, ",");
}
$sql2 = "select destination.destination_description as description from destination where destination.name='".$destination."'";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row = $result2->fetch_assoc()) {
$response->speech = $destination." is the ".$row["description"].". ".$insert . "are some of the places you can try.";
echo json_encode($response);
}
}
This $insert array doesn't give the expected result. It gives just "N".
You can remove the line foreach ($locations as $location)
Your while already fetch all rows to locations so you just need to append your locations[‘name’]. Your foreach has retrieved each character in the string $locations
I think you should look at explode function that might help you Explode function in php
If i understand your problem. You need to use implode() function of php.Try this:-
if ($result1->num_rows > 0) {
$ImplodeArray =array();
while($locations = $result1->fetch_assoc()) {
foreach ($locations as $location) {
$ImplodeArray[] = $location['name'];
}
}
$insert = implode(',',$ImplodeArray);
echo $insert;
}
Hope it helps!
My code is resulting in a output like this
Title1,1,Title2,2,
How can i remove the last comma from this?
$comma = ",";
$query = "SELECT name, listid FROM playlist ORDER BY id DESC";
$result = $mysqli->query($query);
while(list($name, $listid) = $result->fetch_row()) {
echo $name;
echo $comma;
echo $listid;
echo $comma;
}
$lines = array();
while(list($name, $listid) = $result->fetch_row()) {
$lines[] = "$name,$listid";
}
echo implode(',', $lines);
Don't manually echo commas. Just make an array, then implode it.
echo implode(',', array($name, $listid));
Use trim
<?php
$str = "1,2,3,4,5,";
echo trim($str, ",");
Output
1,2,3,4,5
You can try this:
$query = "SELECT name, listid FROM playlist ORDER BY id DESC";
$result = $mysqli->query($query);
$data = array();
while(list($name, $listid) = $result->fetch_row()) {
$data[] = $name.",".$listid;
}
echo implode(',', $data);
try this
$comma = ",";
$html = "";
$query = "SELECT name, listid FROM playlist ORDER BY id DESC";
$result = $mysqli->query($query);
while(list($name, $listid) = $result->fetch_row()) {
$html .= $name.$comma.$listid.$comma;
}
echo substr($html, 0, strlen($html) - 2);
I would recommend going with Joseph Silber's answer, although this would work :
$comma = ",";
$k=0;
$query = "SELECT name, listid FROM playlist ORDER BY id DESC";
$result = $mysqli->query($query);
while(list($name, $listid) = $result->fetch_row()) {
if($k!=0){
echo $comma;
}
echo $name;
echo $comma;
echo $listid;
$k++;
}
$result = mysqli_query($con,$query);
$num_rows= mysqli_num_rows($result);
$ctr=0;
while($row=mysqli_fetch_array($result)){
$ctr++;
echo $row['example'];
if($ctr!=$num_rows){
echo ',';}
else{
echo '';
}
}
I want to make a Navigation with 2 levels.
My Code so far
<?php
$sql = ("SELECT name, id, pid FROM tl_table WHERE pid='' ORDER BY name");
$result = mysql_query($sql);
$list = array();
while ($row = mysql_fetch_assoc($result)) {
$list[] = $row;
}
foreach ($list as $kat) {
echo '<li>' . $kat['name'] . '</li>';
}
?>
Nested Sets are at the moment too tricky for me.
I want at the end this.
<li>$kat['name']
<li>$kat['name'] from PID</li>
</li>
MySQL:
http://i46.tinypic.com/35052m0.png - IMG
No I want to get the things our of the MySQL DB see the image Link.
MySQL:
id—–pid——name
1——0——–name1
2——0——–name2
3——0——–name3
4——3——–name3.1
5——3——–name3.2
<?php
$sql = ("SELECT name, id, pid FROM tl_table WHERE pid='' ORDER BY name");
$result = mysql_query($sql);
$list = array();
while ($row = mysql_fetch_assoc($result)) {
$list[$row['id']] = $row;
$sql = ("SELECT name, id, pid FROM tl_table WHERE pid='".$row['id']."' ORDER BY name");
$res = mysql_query($sql);
while($rw = mysql_fetch_assoc($res)){
$list[$row['id']]['sub'][] = $rw;
}
}
echo "<pre>";
print_r($list);
?>
I have a collection of results from a MySQL query being fetched in a loop.
I need to store them as a single variable with a space in between each result.
$result = mysql_query("SELECT Names FROM table");
while($row = mysql_fetch_array($result)){
echo $row['Names'] . " ";
}
So later, I can call 'echo $Names;' and get the string of names with a space in between.
ex) Clinton Bush Huckabee Romney etc....
Thanks for your help!
$Names = '';
$result = mysql_query("SELECT Names FROM table");
while($row = mysql_fetch_array($result)){
$Names.= $row['Names'] . " ";
}
$result = mysql_query("SELECT Names FROM table");
$names = "";
while($row = mysql_fetch_array($result)){
$names .= $row['Names'] . " ";
}
echo $names;
First of all, please, stop using mysql extention. It's deprecated. Try mysqli or PDO.
$names = array();
$result = mysql_query("SELECT Names FROM table");
while($row = mysql_fetch_array($result)){
$names[] = $row['Names'];
}
echo implode(' ', $names);
I would use an array and then implode it using a space:
$result = mysql_query("SELECT Names FROM table");
$arr = array();
while($row = mysql_fetch_array($result)) $arr[] = $row['Names'];
echo implode(' ', $arr);
I have two arrays in my php that I want to print. I want them to be concatenated but I don't know how. The array for the $names prints but the description array "$desc" does not. is there any way to print both together?
$query = "SELECT eName FROM Events";
$query2 = "SELECT eDescription FROM Events";
$result = mysql_query($query);
$result2 = mysql_query($query2);
$names = array();
$desc = array();
echo "hello there people!" . $query . " ".$result;
for($i=0; $i<sizeof($result); $i++){
echo $result[$i] ."\n" . $result2[$i];
}
while($entry = mysql_fetch_row($result)){
$names[] = $entry[0];
}
while($entry2 = mysql_fetch_row($result2)){
$desc[] = $entry2[0];
}
echo "Which Event would you like to see?<br>";
$stop = count($names);
//echo $stop . "\n";
$i = 0;
print_r($names);
print_r($desc);
foreach($names as $value){
echo $value . " " . $desc[i] ."<br>";
$i++;
}
Why are you doing two queries to get data from the same source?
$sql = mysql_query("select `eName`, `eDescription` from `Events`");
while($row = mysql_fetch_assoc($sql)) {
echo $row['eName']." ".$row['eDescription']."<br />";
}
Much simpler.
Try this:
foreach($names as $key => $value){
echo $value . " " . $desc[$key] ."<br />";
}
As long as the array $key match, the information will be printed together.