Make a new table row every 2 echoed - php

I'm trying to make a new table row every time a user is echoed.
Here is what I've tried to zero success.
<?
$i = 0;
while($row = mysql_fetch_array($admin, MYSQL_ASSOC)){
if($i <= 2){
$i++;
echo "<tr><td>".$row['id']."</td><td>".$row['ip']."</td><td>".$row['username']."</td><td>".$row['password']."</td><td><img src='uploads/avatars/".$row['avatar']."' style='width:25px;height:25px;' />";
}
else{
echo "<td>".$row['id']."</td><td>".$row['ip']."</td><td>".$row['username']."</td><td>".$row['password']."</td><td><img src='uploads/avatars/".$row['avatar']."' style='width:25px;height:25px;' /></td></tr>"
$i=0;
}
}
?>
Any ideas peeps?

This should work:
<?
$i = 0;
echo "<table><tr>";
while($row = mysql_fetch_array($admin, MYSQL_ASSOC)) {
if($i++ % 2 == 0 && $i > 1) {
echo "</tr><tr>";
}
echo "<td>".$row['id']."</td><td>".$row['ip']."</td><td>".$row['username']."</td><td>".$row['password']."</td><td><img src='uploads/avatars/".$row['avatar']."' style='width:25px;height:25px;' /></td>"
}
echo "</tr></table>";

Related

Fill a table vertically in php

I'd like to fill a table vertically. For example one name on one row, the age on the second row alternately, and limit the table to 4 columns:
Is this possible with this kind of code?
Here is my Code:
<?php
$age = array("26","16","17","19","24","30");
$name = array("adam","andrew","tim","mike","don","eddy");
echo "<table border=1>";
for($i=0;$i<count($age);$i++)
{
if ($i > 0 && $i % 4 == 0)
{
echo "</tr><tr>";
}
echo "<td>";
echo $name[$i];
echo "</td>";
echo "<td>";
echo $age[$i];
echo "</td>";
}
echo "</tr>";
?>
Please try this. It is not the most elegant way but will give you an idea and maybe improve your answer. I will suggest you maybe use multi-dimensional array to store age and name together.
$age = array("26","16","17","19","24","30");
$name = array("adam","andrew","tim","mike","don","eddy");
$ageChunks = array_chunk($age, 4); //divide age array into chunks of 4
$nameChunks = array_chunk($name, 4); //divide name array into chunks of 4
//print table data
function printData($td)
{
echo "<td> ".$td." <td>";
}
//print table row. Assumption here is $nameChunks and $ageChunks are same length.
function printRow($nameChunks, $ageChunks)
{
foreach ($nameChunks as $key=>$val) {
echo "<tr>";
array_map("printData", $nameChunks[$key]);
echo "</tr><tr>";
array_map("printData", $ageChunks[$key]);
echo "</tr>";
}
}
//print table
function printTable($nameChunks, $ageChunks)
{
echo "<table>";
printRow($nameChunks, $ageChunks);
echo "</table>";
}
printTable($nameChunks, $ageChunks);
you need to change condition
if ($i > 0 && $i % 4 == 0) to if ($i > 0 && $i % 2 == 0) because you print 2 columns per iteration

How to echo every after 9th iteration of a loop? First echo only counted 8 items

My code is below:
function portfolio_gallery() {
global $conn;
$query = $conn->query("SELECT codename, namegroup, features, title, showimage FROM portfolio ORDER BY id DESC");
if ($query->num_rows > 0) {
// output data of each row
echo '<div>';
$i = 0;
while($row = $query->fetch_assoc()) {
$i++;
if ($row["showimage"]) {
if($i % 9 == 0){
echo '</div><div>';
}
echo '<a class="imgpop" href="images/portfolio/large/'.$row["codename"].'.jpg" rel="'.$row["namegroup"].'" title="'.$row["title"].' - '.$row["features"].'"><img src="images/portfolio/thumb/'.$row["codename"].'.jpg" alt="'.$row["title"].'" width="348"/><span class="imgpop-caption">'.$row["title"].'</span></a>';
}
}
echo '</div>';
}
}
portfolio_gallery();
I wanted to echo </div><div> for every after 9th item of the loop but every time I executed the code, the first echo only happened after 8 items instead of 9, but the rest was every 9th.
You have to increment
$i
after
if($i % 9 == 0)
follow the syntax example i worked out its working
<?php
$j=0;
for($i=0;$i<=50;$i++)
{
if($j==9)
{
echo $j.'hioiiiiiii<br/>'; //echo "</div><div>";
$j=-1;
}
$j++;
}
?>
Please try this :)
<?php
function portfolio_gallery() {
global $conn;
$query = $conn->query("SELECT codename, namegroup, features, title, showimage FROM portfolio ORDER BY id DESC");
if ($query->num_rows > 0) {
// output data of each row
echo '<div>';
$i = 0;
while($row = $query->fetch_assoc()) {
if ($row["showimage"]) {
if($i % 9 == 0){
echo '</div><div>';
}
echo '<a class="imgpop" href="images/portfolio/large/'.$row["codename"].'.jpg" rel="'.$row["namegroup"].'" title="'.$row["title"].' - '.$row["features"].'"><img src="images/portfolio/thumb/'.$row["codename"].'.jpg" alt="'.$row["title"].'" width="348"/><span class="imgpop-caption">'.$row["title"].'</span></a>';
}
$i++;
}
echo '</div>';
}
}
declare $i = 1 and Write $i++ at the end of while loop.
if ($query->num_rows > 0) {
// output data of each row
echo '<div>';
$i = 1; // declare $i = 1 here
while($row = $query->fetch_assoc()) {
if ($row["showimage"]) {
if($i % 9 == 1){ // 10 , 19 ,28
echo '</div><div>';
}
echo '<a class="imgpop" href="images/portfolio/large/'.$row["codename"].'.jpg" rel="'.$row["namegroup"].'" title="'.$row["title"].' - '.$row["features"].'"><img src="images/portfolio/thumb/'.$row["codename"].'.jpg" alt="'.$row["title"].'" width="348"/><span class="imgpop-caption">'.$row["title"].'</span></a>';
}
$i++; // increment at end of the loop
}
echo '</div>';
}
I was able to solve it by modifying the condition:
So instead of: if($i % 9 == 0) {...}
I used: if($i!=0 && $i % 9 == 0) {...}
And also placing $i++ at the end of while loop.

PHP showing data

I am showing images which are stored in my DB.
See code below
$num_rows = mysql_num_rows($result41);
for ($i = 1; $i <= mysql_num_rows($result41); $i++)
{
$row = mysql_fetch_array($result41);
$upload_id = $row ['upload_id'];
$file = $row ['FILE_NAME'];
echo"
<td>
<a href='image.php?id=$upload_id&gallery=$id'><center>
<img src='uploads/$file' alt='$name Gallery' title='$name Album' class='resize'>
</td>";
}
if ($i % 0 == 4) {
echo '</tr>'; // it's time to move to next row
}
My question is, after showing 4 columns how do I move onto another row? (only 4 images per row)
I have if ($i % 0 == 4) in my script but doesn't seem to be working?
Thanks
Like this :
for ($i = 1; $i <= mysql_num_rows($result41); $i++)
{
$row = mysql_fetch_array($result41);
$upload_id = $row ['upload_id'];
$file = $row ['FILE_NAME'];
if($i % 4 == 0) echo "<tr>";
echo"
<td>
<a href='image.php?id=$upload_id&gallery=$id'><center>
<img src='uploads/$file' alt='$name Gallery' title='$name Album' class='resize'>
</td>";
if($i % 4 == 3) echo "</tr>";
}
And look at PHP PDO for your Mysql access into Google ;)
And start you loop (for) at 0 ?
This is how I would do it:
$num_rows = mysql_num_rows($result41);
$k = 1;
for ($i = 1; $i <= mysql_num_rows($result41); $i++){
$row = mysql_fetch_array($result41);
$upload_id = $row ['upload_id'];
$file = $row ['FILE_NAME'];
echo"
<td>
<a href='image.php?id=$upload_id&gallery=$id'><center>
<img src='uploads/$file' alt='$name Gallery' title='$name Album' class='resize'>
</td>";
if($k == 4){
$k = 1
echo "</tr><tr>";
} else {
$k++;
}
}
The $k variable counts to 4 and resets every time.
While it resets, it also ends and creates a new row in the table.

Render SQL query results as an HTML table

I need help rendering data in PHP:
I want to use the results of a SQL query to populate a table, displaying images in a 4-column grid. Here's what I've written so far:
$result = mysql_query("SELECT * FROM gallery ORDER BY id ASC ");
while($row = mysql_fetch_array($result))
{
echo "<td align=\"center\" ><img src=\"upload_gallery/thumbnail/sml_".$row['image_name']."\" width=\"200\" height=\"170\" /></td>";
}
<table><tr>
<?$result = mysql_query("SELECT * FROM gallery ORDER BY id ASC ");
$result =mysql_fetch_array($result)
$count=0;
for($row =0; $row<count($result); $row++)
{
$count++
if($count==4){
echo '</tr><tr>';
$count=0;
}
echo "<td align=\"center\" ><img src=\"upload_gallery/thumbnail/sml_".$row['image_name']."\" width=\"200\" height=\"170\" /></td>";
}
?>
</tr>
</table>
You can do a for loop as was suggested by Frederick:
$num_rows = mysql_num_rows($result);
for($i = 0; $i < $num_rows; $i++)
{
// Check if beginning of row
if($i % 4 == 0)
{
//If not the first row, end the last row first
if($i > 0)
{
echo "</tr>";
}
echo "<tr>";
}
$row = mysql_fetch_assoc($result);
//Output each individual image
echo "<td> {*EACH IMAGE code*}</td>";
}
That should give you the idea.
Try this one:
$arr =array("Test1","Test2","Test3","Test4","Test5","Test6","Test7","Test8","Test9","Test10");
echo "<table><tr>";
for($i = 1 ; $i <= count($arr) ; $i++)
{
echo"<td>".$arr[$i-1]."</td>";
$last = $i % 4 == 0? "<tr/><tr>":"";
echo $last;
}
echo "</table>";
Try this, hope it works for you,
while($row = mysql_fetch_array($result))
{
$data[]= $row['image_name'];## Pass the image data to new array
}
$ImagePerRow = 4;### Set the value of image per table row
echo "<table border='1'><tr>";
foreach($data as $key => $ImageName)
{
if(($key+1) == $ImagePerRow)## if key index+1 is equal to ImagePerRow
{
echo "<td align=\"center\" ><img src=\"upload_gallery/thumbnail/sml_".$ImageName."\" width=\"200\" height=\"170\" /></td></tr><tr>";## then close the table row and start a new table row
$ImagePerRow+=4;### Add a value of 4 in order to increment the imageperRow value on the next loop
}else{
echo "<td align=\"center\" >".$ImageName."</td>";### else echo the normal table data
}
}
echo "</tr></table>";

Break mysql_num_rows into 6x4

Pretty much I have a code like below that I'm trying to add </tr><tr> to after every 6 results.
echo "<table><tr>";
$query="SELECT * WHERE id='$id' ORDER BY date ASC";
$result=mysql_query($query);
if (mysql_num_rows($result) > 0) {
while($rts = mysql_fetch_array($result)){
$cdata1 = $rts['cdata1'];
$cdata2 = $rts['cdata2'];
echo "<td>$cdata1 and $cdata2</td>";
}
}else{
echo "<td>no results</td>";
}
echo "</tr></table>";
echo "<table><tr>";
$query="SELECT * WHERE id='$id' ORDER BY date ASC";
$result=mysql_query($query);
$i = 0;
if (mysql_num_rows($result) > 0) {
while($rts = mysql_fetch_array($result)){
$cdata1 = $rts['cdata1'];
$cdata2 = $rts['cdata2'];
echo "<td>$cdata1 and $cdata2</td>";
if(++$i % 6 == 0) {
echo '</tr><tr>';
}
}
}else{
echo "<td>no results</td>";
}
echo "</tr></table>";
UPD:
Whats means if(++$i % 6 == 0) code:
++$i equals $i = $i + 1;
$i % 6 means $i modulo 6
If $i modulo 6 equals 0 then echo </tr><tr>
So we can write it as:
$i = $i + 1;
if($i % 6 == 0) {
echo '</tr><tr>';
}
http://php.net/manual/en/internals2.opcodes.mod.php
http://php.net/manual/en/language.operators.increment.php

Categories