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
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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.