Jump to content

Pls help me with while statement


ibinod

Recommended Posts

Hi, wt i am trying to do is breaking a while loop, it's executed with a mysql query

my mysql database consist of a table called pics and has a colum id which consist around 20 feilds and i need to execute in this way so the output becomes like this

 

<tr>

     <td></td>

     <td></td>

     <td></td>

     <td></td>

     <td></td>

</tr>

<tr>

     <td></td>

     <td></td>

     <td></td>

     <td></td>

     <td></td>

</tr>

<tr>

     <td></td>

     <td></td>

     <td></td>

     <td></td>

     <td></td>

</tr>

 

 

till now i did it in this way

	<table width="860px" align="center" cellpadding="0" cellspacing="0" border="0" class="randimg">
	<tr>
<?php
$sql = mysql_query("SELECT * FROM pics ORDER BY id DESC");
while($pics = mysql_fetch_array($sql)) {
$id = $pics['id'];
$cat = $pics['cat'];
$pic = $pics['pic'];

echo "<td></td>";
}
?>
</tr>
</table>

it gives me the output like this

 

<tr>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

</tr>

 

 

pls help me i out to break this while loop after every 5 ids from the pics table and 5 <td> </td> tags are executed and again i want to continue it from there after inserting  <tr> tag

 

Link to comment
https://forums.phpfreaks.com/topic/113330-pls-help-me-with-while-statement/
Share on other sites

i want it to execute first 5 queries like this

 

<tr>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

</tr>

 

and again continue it the same way from the 6th query

 

<tr>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

</tr>

<tr>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

</tr>

<tr>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

</tr>

 

 

i trired a lot  i only get this

<tr>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

</tr>

 

so i came over here searching for help and solution

Do you want:

	
<table width="860px" align="center" cellpadding="0" cellspacing="0" border="0" class="randimg">
<?php
$sql = mysql_query("SELECT * FROM pics ORDER BY id DESC");
$i = 0;
while($pics = mysql_fetch_array($sql)) {
echo "<tr>";
$id = $pics['id'];
$cat = $pics['cat'];
$pic = $pics['pic'];
echo "<td></td>
</tr>";
}
?>
</table>

You're not explaining yourself very well.

You need to count how many items your outputing. Something like this:

<table width="860px" align="center" cellpadding="0" cellspacing="0" border="0" class="randimg">
<?php
$sql = mysql_query("SELECT * FROM pics ORDER BY id DESC");
$tmp = array();
$tbl = array();
while($pics = mysql_fetch_assoc($sql)) {
     $tmp[] = $pics['id'] . ' ' . $pics['cat'] . . $pics['pic'];
     if (count($tmp) == 5) {
          $tbl[] = '<tr><td>' . implode('</td><td>',$tmp) . '</td></tr>';
          $tmp = array();
     }
}
if (!empty($tmp))
    $tbl[] = '<tr><td>' . implode('</td><td>',$tmp) . '</td></tr>';
implode("\n",$tbl);
?>
</table>

 

Note: not tested.

 

Ken

Hello kenrbnsn,

thanx a lot for ur reply but i couldn't get ut wirj

actually i want the query to execute all the rows from my mysql table, i don't want to limit it, currently there are 20 rows but they will increase once more stuffs are added so i don't want to limit it with a specified value

 

all i want to is insert <tr> </tr> tag after each 5 <td></td> tags.

 

thanx a lot wildteen, i again tried making up and i see it really works as i wanted, thank you very much ken for the help thanx this is really wonderful, and it's a great community came here today and got my solution within minutes, it's just wonderful thank you very much again,  :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.