Php mysql print row in 3 divs - php

<div id="first"></div>
<div id="second"></div>
<div id="third"></div>
Is there any way to print each third database row in a div? So every div gets one row. Like a newsfeed.
I want the first print to be in the fist, second in the second, third in the third, fourth in the first, fifth in the second and so on.
Is this possible? Like skip every third or something. With php

You have to write the code like below. May this helps you.
$result = mysql_query("select * from table_name");
$k=1;
while($row = mysql_fetch_array($result)){
if($k==1){
echo '<div id="first"></div>';
}
if($k==2){
echo '<div id="second"></div>';
}
if($k==3){
echo '<div id="third"></div>';
$k=1;
}
$k++;
}
OR
$result = mysql_query("select * from table_name");
$k=1;
while($row = mysql_fetch_array($result)){
if($k==1){
$div_id = "
}
if($k==2){
$div_id = "second";
}
if($k==3){
$div_id = "third";
$k=1;
}
echo '<div id="'.$div_id.'"></div>';
$k++;
}

Related

multiple echo statements in one echo?

I am relatively new to PHP and want to know a more efficient way of echo'ing the following in less steps or even just one statement.
$sql = "SELECT * FROM comments";
$result = OpenCon()->query($sql);
while( $row = $result->fetch_assoc()) {
echo "<div class='card'><p class: card-text>";
echo $row['uid']."<br>";
echo $row['date']."<br>";
echo $row['message']."<br><br>";
echo "</p></div>";
As #droopsnoot has commented on above, you should try doing the next thing here.
while( $row = $result->fetch_assoc()) {
echo "<div class='card'><p class: card-text>". $row['uid']."<br>" . $row['date']."<br>".$row['message']."<br><br>". "</p></div>";
}
It should work perfectly.
Another option is to close php tag (?>) and use html markup with <?=$var?>:
<?php
$sql = "SELECT * FROM comments";
$result = OpenCon()->query($sql);
while( $row = $result->fetch_assoc()) {?>
<div class="card">
<p class="card-text">
<?=$row['uid']?><br>
<?=$row['date']?><br>
<?=$row['message']?><br><br>
</p>
</div>
<?php
} // end while

PHP incomplete result table from sql query

I am new to web development and starting learning about CRUD.
My question is that while I successfully show the table listing 3 product on 1 row, that on the second row the product no 4 are missing and skipping to the product no 5 and keep missing every last 3 row.
function getData(){
global $connect;
$query = "SELECT id, name, category, price, image, info FROM product_data";
$results = mysqli_query($connect, $query) or die(mysql_error());
echo "<table border = 0 >";
$x=1;
echo "<tr class='homepagetable'>";
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
if($x<=3){
$x = $x + 1;
extract($row);
echo "<td class='homepagetable'>";
echo "<a href=itemdetails.php?id=$id>";
echo '<img src=img/' . $image . ' style="max-width:220px;max-height:240px;width:220px;height:240px;"></img><br/>';
echo '<div class="truncate">'. $name .'</div><br/>';
echo "</a>";
echo 'Rp.'.$price .'<br/>';
echo "</td>";
} else {
$x=1;
echo "</tr><tr>";
}
}
echo "</table>";
}
Thanks in advance!
Your problem is that you have a wrong condition here:
if($x <=3)... else{...}
Change the if/else to this:
if($x <3){$x++;}
//...do something
if($x == 3){
$x = 1;
//Close and open tr
}
And you need to close the <img> tag, and the last <tr> tag outside of the loop
Perhaps you can write it like this:
function getData()
{
global $connect;
$query = 'SELECT id, name, category, price, image, info FROM product_data';
$result = mysqli_query($connect, $query) or die(mysql_error());
echo '<table border="0">';
$x=0;
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
if($x % 3 == 0)
{
// number of articles is a multiple of 3 - so close the row
if($x) echo '</tr>'; // but only if there was at least 1 article
echo '<tr class="homepagetable">';
}
$x ++;
echo '<td class="homepagetable"><a href="itemdetails.php?id='.$row['id'].'">
<img src="img/'.$row['image'].'" style="max-width:220px;max-height:240px;width:220px;height:240px;"><br/>
<div class="truncate">'.$row['name'].'</div><br/>
</a>Rp. '.$row['price'].'<br/>
</td>';
}
if($x) echo '</tr>'; // only close the row if there was at least 1 article
echo '</table>';
}

Failed to iterate over selected rows

The code is supposed to get a row at each loop and build td elements for each data piece. However, it only gets some rows while missing others even though all rows were selected:
$query = "select * from category";
$ret = mysql_query($query, $conn);
while ($rows = mysql_fetch_assoc($ret)) {
echo "<tr>";
foreach ($rows as $val) {
echo "<td>{$val}</td>";
}
}
mysql_fetch_assoc returns an associative array. To display each returned row, do something like this (taken directly from the php.net page for mysql_fetch_assoc
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
Try removing the curly braces like this:
echo "<td>$val</td>";
Beside you didn't close the <tr> tag. So you should do this:
$query = "select * from category";
$ret = mysql_query($query, $conn);
while ($rows = mysql_fetch_assoc($ret)) {
echo "<tr>";
foreach ($rows as $val) {
echo "<td>$val</td>";
}
echo"</tr>";
}
Now like Jack Albright said you should consider various columns of your table to display specific data. Upon that the while() is already a look, I think you don't need to add any foreach() inside. SO your final code should look like this:
$query = "select * from category";
$ret = mysql_query($query, $conn);
while ($rows = mysql_fetch_assoc($ret)) {
echo "<tr>";
echo "<td>$rows['columnName']</td>";
echo "</tr>";
}

php query adding a div between every third item echod to page

I cant seem to figure out how to implement any examples I found online of how to us a counter so that ever 3rd echo of " $row['item']" has a div in between it.
$result = mysql_query("SELECT * FROM table")
while($row = mysql_fetch_array( $result )) {
echo $row['item'] ;
}
When you want to do something every x loops, I find the easiest method is to use a modulo/modulus operator:
for($i=0;$i<20;$i++)
{
if($i%3==0)
{
echo "This is the third time round...";
}
}
you can easily implement this into a loop while fetching rows from the database.
$result = mysql_query("SELECT * FROM table")
$i=0;
while($row = mysql_fetch_array( $result ))
{
echo $row['item'] ;
if($i%3==0)
{
echo "Do your DIV stuff here...";
}
$i++;
}
Try
$result = mysql_query("select * from table")
$i=0;
while($row = mysql_fetch_array( $result ))
{
echo $row['item'] ;
if($i%3==0)
{
echo "<div>Your div content</div>";
}
$i++;
}
I would also suggest to go through for/while/foreach loop as well

Displaying data horizontally using php and mysql

Hi I'm attempting to display data retrieved from a mysql table horizontally in an html table using php. The code below works well except for the fact that it leaves out the first record (starts at the second record) in my database. I'm sure it has something to do with the counter but I can't seem to figure out how to get it to stop doing this. If anyone can point out my error I'd really appreciate it!
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
$i = 0;
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
if ($i==0) {
echo "<tr>\n";
}
echo "\t<td align=\center\">$first_name</td>\n";
$i++;
if ($i == $items) {
echo "</tr>\n";
$i = 0;
}
}//end while loop
if ($i > 0) {
for (;$i < $items; $i++) {
echo "<td> </td>\n";
}
echo '</tr>';
}//end ($i>0) if
echo '</table>';
}else {
echo 'no records found';
}
try and remove the 1st
$row = mysql_fetch_array($result);
you are calling it twice, that's why it skips 1 row in your while loop
try this simpler.
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
echo "<tr>";
for ($i=0 ; $i <= $items ;$i++) {
echo "<td align='center'>".$first_name."</td>";
}
}//end while loop
echo "</tr>";
echo '</table>';
}else{ echo 'no records found'; }
I have run into this issue before. Try the do while loop instead. Example
do {
// code
} while($row = mysql_fetch_array($result)); //end while loop
$row = mysql_fetch_array($result);
1) remove this line of code from ur scripts
2) only use while loop code instead.

Categories