please help, i am learning json, but i find it difficult to insert code echo. If you know the answer please help me
My Code
if ($result) {
$arr['news'] = '';
$idterakhir = '';
$query = $pdo->query($sql);
while ($row = $query->fetch(PDO::FETCH_OBJ)) {
// change this into echo
$arr['news'] .= '<div class="card" news_id="'.$row->news_id.'">'.$row->news_id.'<br>'.$row->news.'</div>';
// change this into echo
$idterakhir = $row->news_id;
}
$arr['idterakhir'] = $idterakhir;
$arr['end'] = false;
} else {
$arr['end'] = true;
}
echo json_encode($arr);
into :
echo "
<div class='card' news_id='$id'> $id <br> $title </div>
";
while ($row = $query->fetch(PDO::FETCH_OBJ)) {
$id2 = $row['news_id'];
$id = <div class="card" news_id="'.$id2.'">
$news = $row['news'];
$title = $id.$id2.<br>'.$news.'</div>
$arr['news'] .= title;
echo $arr['news'];
$idterakhir = $row->news_id;
}
This should print $arr['news'] for each circle of the while.
If you need to echo out the contents of $arr['news'], just do:
echo $arr['news'];
Related
I'm trying to echo a bit of html within a while loop. I'm getting my stuff with PDO and PDO_ASSOC.
This is what I have:
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$book_id = $row['id'];
$book_title = $row['title'];
$book_image = $row['image'];
$book_amz = $row['amazon'];
$book_desc = $row['description'];
$book_rating = $row['rating'];
$book_date = $row['date'];
$book_author = $row['author'];
$book_categorie = $row['categorie'];
$text = "ID: ' . $book_id . '";
}
return $text;
But it gives me only one row of the table. I even tried fetchAll, but it gives me nothing.
So making the assumption that the only element ever seen is the last element it is because what your are returning is being overwritten each loop. There are a few options to resolve this. The simplest is:
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
$text = "";
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$book_id = $row['id'];
$book_title = $row['title'];
$book_image = $row['image'];
$book_amz = $row['amazon'];
$book_desc = $row['description'];
$book_rating = $row['rating'];
$book_date = $row['date'];
$book_author = $row['author'];
$book_categorie = $row['categorie'];
//String concatenation of text will
//give you one big string at the end to return.
$text .= "ID: '{$book_id}'";
}
return $text;
However this will not work well with your real bootstrap html. You need to make sure that the columns add up right.
You will need something a bit more intuitive
Using the actual code it would look something like
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
$bookEcho = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$bookEcho[] = '<div class="col-md-3">
<div class="thumbnail">
<span>' . $book_title . '</span>
<img src="' . $book_image . '">
<div class="book-options">
<span>Bewertung</span><br/>
' . $stars . '
Jetzt lesen
</div>
</div>
</div>';
}
return $bookEcho;
Now in your function what ever it is you can do something like (this is not the most elegant thing I have ever written but should get the job done):
$cols = 4;
$colCount = 1;
foreach ($bookEcho as $book){
if($colCount == 0){//create a row}
echo $book;
$coolCount++;
if($colCount == 0){end a row}
if($colCount == 4){ $colCount = 0;}
}
The problem is that you're overwriting your values in your while loop. The loop is being executed once for each entry in the database, but only the last one will be returned. Instead you want to use arrays:
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
$books = [];
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$book['id'] = $row['id'];
$book['title'] = $row['title'];
$book['image'] = $row['image'];
$book['amz'] = $row['amazon'];
$book['desc'] = $row['description'];
$book['rating'] = $row['rating'];
$book['date'] = $row['date'];
$book['author'] = $row['author'];
$book['categorie'] = $row['categorie'];
$book['text'] = "ID: ' . {$book['id']} . '"; // << Not sure if this is what you actually want. If not, adjust accordingly.
// Append the above values to the $books array
$books[] = $book;
}
return $books;
Use a foreach:
$books = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($books as $book){
$book_id = $book['id'];
$book_title = $book['title'];
// ...
}
I already try many ways but the value didn't show in dropdown list
Here, this is my code. can you suggest me anything that i was wrong
<?php
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
$row = mysqli_fetch_assoc( $result );
$pos = 0;
echo "<select name=Pname >";
while($pos <= count ($row)){
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
$pos++;
}
echo "</select>";?>
And i write as .php file. Thanks for your help.
Try this out:
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output .= '<option value="' . $project_no . '">' . $project_name . '</option>";
}
}
Then inside of your HTML, print your $output variable inside of your <select> element:
<select>
<?php
print("$output");
?>
</select>
It should print all options for every row that you have requested from the database.
Hope this helps :)
Try this:
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
echo "<select name=Pname >";
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
}
echo "</select>";
}
This is the result code that i can run it. I put this code in a form code of html
$result = mysqli_query($con,"SELECT * FROM project"); ?>
<?php
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
echo " <select name = Pname>";
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output = "<option value=" . $project_no . "> ". $project_name ." </option>";
print("$output");
}
echo " </select>";
}
?>
Thank you every one for helping me ^^
Currently I am using following code to get data sorted by starting letter of name, if you run this code you will get what i am trying to create
<?php
$dirs = array('Aname1','Aname2','Aname3','A Nmae','Bname ','Cname','Cardiff','Dname','Dname',);
$cur_let = null;
foreach ($dirs as $dir) {
if ($cur_let !== strtoupper(substr($dir,0,1))){
$cur_let = strtoupper(substr($dir,0,1));
echo "<li class=\"title\">".$cur_let."</li>";
}
echo "<li class=\"clear\">
<div class=\"name\">".$dir."</div>
<div class=\"mobile\"></div>
<div class=\"telephone\"></div>
<div class=\"email\"></div>
<div class=\"action\">edit | delete</div>
<div class=\"clear\"></div>
</li>";
}
but how to use above loop inside following to get vales from database and it should be display like (I want highlight first letter) http://i.stack.imgur.com/bLHVD.jpg
$query = "SELECT * FROM phone_number";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$names = $row['name'].",";
}
?>
With MySQLi:
$last_letter = null;
$query = "SELECT name FROM phone_number ORDER BY name";
$sql = $mysqli->query($query);
while($row = $sql->fetch_assoc()) {
$first_letter = substr(ucfirst($row['name']), 0, 1);
if($last_letter != $first_letter) {
$last_letter = $first_letter;
echo '<div class="letter">', $first_letter, '</div>';
}
echo ucwords($row['name']), '<br />';
}
With mysql_* deprecated functions:
$last_letter = null;
$query = "SELECT name FROM phone_number ORDER BY name";
$sql = mysql_query($query);
while($row = mysql_fetch_array($sql)) {
$first_letter = substr(ucfirst($row['name']), 0, 1);
if($last_letter != $first_letter) {
$last_letter = $first_letter;
echo '<div class="letter">', $first_letter, '</div>';
}
echo ucwords($row['name']), '<br />';
}
Try something like this:
$query = "SELECT * FROM phone_number ORDER BY name DESC";
$result = mysql_query($query) or die(mysql_error());
$lastLetter = '';
$html = '<ul>';
while ($row = mysql_fetch_array($result)) {
$name = $row['name'];
if (strtoupper($name[0]) !== $lastLetter) {
if ($lastLetter !== '')
$html .= '</ul></li>';
$lastLetter = strtoupper($name[0]);
$html .= '<li class="title">' . $lastLetter;
$html .= '<ul>';
}
$html .= '<li>' . $name . '</li>';
}
$html .= '</ul></li></ul>';
Im trying to create this thing that if its get nothing from a array it would bring up a default picture but its not working out the way i want it to. It using every array but i want to separate each away.
$result = mysql_query("SELECT * FROM `im_album` WHERE username = '".$user_data['username']."' ");
$d_name = array();
$d_amount = array();
$d_cover = array();
while(($row = mysql_fetch_assoc($result))) {
$d_name[] = $row['name'];
$d_mode[] = $row['mode'];
$d_amount[] = $row['amount'];
$d_cover[]= $row['cover'];
}
$p_cover = implode($d_cover) ;
if ( $p_cover == "") {
$p_cover = "cpages/img/default_pic.jpg";
}
else
{
$p_cover= "userdata/".$user_data['username']."/profilepic/".$p_cover;
}
<img style="margin:2pt" src="<?php echo $p_cover ?>" height="250" width="200" />
$result = mysql_query("SELECT * FROM `im_album` WHERE username = '".$user_data['username']."' ");
while ($row = mysql_fetch_assoc($result)) {
$d_name = $row['name'];
$d_mode = $row['mode'];
$d_amount = $row['amount'];
$d_cover = $row['cover'];
if ( $d_cover == "") {
$p_cover = "cpages/img/default_pic.jpg";
}
else
{
$p_cover= "userdata/".$user_data['username']."/profilepic/".$d_cover;
}
echo '<img style="margin:2pt" src="'.$p_cover.'" height="250" width="200" />';
}
1) I want to save case 1 value to an array. Return this array and pass it to a function. The code become case 2, but no result come out, where is the problem?
2) In function display_urls, i want to echo both $url and $category. What should i do in IF condition or add another line of code?
function display_urls($url_array)
{
echo "";
if (is_array($url_array) && count($url_array)>0)
{
foreach ($url_array as $url)
{
echo "".$url."";
echo "".$category."";
}
}
echo "";
}
case 1: work fine
$result = oci_parse($conn, "select * from bookmark where username ='$username'");
if (!$result){ $err = oci_error(); exit; }
$r = oci_execute($result);
$i=0;
echo "";
while( $row = oci_fetch_array($result) ){
$i++;
echo "";
echo "".$row['USERNAME']."";
echo "".$row['BM_URL']."";
echo "".$row['CATEGORY']."";
echo "";
}
echo "";
case 2:
$url_array = array();
while( $row2 = oci_fetch_array($result, OCI_BOTH)){
$i++;
$url_array[$count] = $row[0];
}
return $url_array;
I think you probably want something like this:
function display_urls($url_array)
{
echo "";
if (is_array($url_array) && count($url_array)>0)
{
foreach ($url_array as $url)
{
echo "".$url['BM_URL']."";
echo "".$url['CATEGORY']."";
}
}
echo "";
}
$result = oci_parse($conn, "select * from bookmark where username ='$username'");
if (!$result){ $err = oci_error(); exit; }
$r = oci_execute($result);
$url_array = array();
while( $row = oci_fetch_array($result, OCI_ASSOC)){
$url_array[] = $row;
}
display_urls($url_array);
This will store all the information on the URLs in $url_array with a lookup by column name.