Click to See Complete Forum and Search --> : [RESOLVED] Add a row
raj_2006
06-29-2007, 09:45 AM
Hi,
Can you please tell me how i can add a single row to this code:
$i=1;
$sql=mysql_query("select * from cat") or die(mysql_error());
while($row=mysql_fetch_array($sql))
{
if($i==1)
{
?>
<tr><td><a href=""><img src="../catpic/<?=$row[filename];?>" height=100 width=100></a></td>
<?
$i++;
}
elseif($i>1 && $i<2)
{
?>
<td><a href=""><img src="../catpic/<?=$row[filename];?>" height=100 width=100></a></td>
<?
$i++;
}
else
{
?>
<td><a href=""><img src="../catpic/<?=$row[filename];?>" height=100 width=100></a></td></tr>
<?
$i=1;
}
}
Thanks in advance......Raj
MrCoder
06-29-2007, 10:05 AM
Use a mathematical MOD (%) operation on $i and don't reset it back to 0.
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0
7 % 3 = 1
8 %......
So for example..
<?php
$i = 1;
?>
<tr>
<?php
$sql=mysql_query("select * from cat") or die(mysql_error());
while($row=mysql_fetch_array($sql))
{
?>
<td><td><a href=""><img src="../catpic/<?=$row[filename];?>" height=100 width=100></a></td>
<?php
if(($i % 3) === 0)
{
?>
</tr><tr>
<?php
}
$i++;
}
?>
</tr>
Now can you make it work out how many coloums are left to draw and place the correct amount of missing <td>'s in?
raj_2006
06-29-2007, 11:22 AM
Hi
Thanks a lot for your suggestion.
I will go through it now....I willlet you know the output
Thanks again...Raj
raj_2006
06-29-2007, 01:36 PM
Hi
Sorry the least I have understood that there are a breaking of 3 records at each interval.
I need to put the <td> as well as a extra <tr>
Can you plz help me out
Thanks again
Raj
temp.user123
06-29-2007, 02:16 PM
You mean you want to change this part:
?>
</tr><tr>
<?php
to this?
?>
</tr>
<tr><td colspan="3"> </td></tr>
<tr>
<?php
raj_2006
07-01-2007, 02:48 PM
Hi
Sorry to reply in late
I am not understanding the code fully.Also I tried to add a <tr> but it is not working.
Pleas help me out.
Thanks
Raj
temp.user123
07-02-2007, 08:02 AM
From the original answer you got, change this part:
if(($i % 3) === 0)
{
?>
</tr><tr>
<?php
}
to this:
if(($i % 3) === 0)
{
?>
</tr>
<tr><td colspan="3"> </td></tr>
<tr>
<?php
}
raj_2006
07-03-2007, 01:18 AM
Hi
if(($i % 3) === 0)
{
?>
</tr>
<tr><td colspan="3">Hello world</td></tr>
<tr>
<?php
}
THe problem is that when I am going to write something inside the <tr>....the <tr> is moving for once(one time).
For every row the first <td> is getting the value.not the 2nd and the 3rd one.
What should i do
temp.user123
07-03-2007, 10:38 AM
Your descriptions leave something to be desired -- though I take it English is not your first language. I'm going to need a better understanding of what you're getting and what you want.
raj_2006
07-03-2007, 11:26 AM
Hi
Thanks for reply.Well what i want is that
My desired output:
(1st column) (2nd column) (3rd column)
(1st row) Image 1 Image 2 Image3
(2nd row) desc desc desc
(3rd row) Image 4 Image 5 Image6
(5th row) desc desc desc
Now what I am getting the output is:
(1st column) (2nd column) (3rd column)
(1st row) Image 1 Image 2 Image3
(2nd row) desc
(3rd row) Image 4 Image 5 Image6
(5th row) desc
Hope it will clear now.I think the 2nd <tr> is not moving after the 1st column.I mean if i do:
if(($i % 3) === 0)
{
?>
</tr>
<tr><td colspan="3"><?=$desc;?></td></tr>
<tr>
<?php
}
then desc is displaying only in the first column.Please suggest.
temp.user123
07-03-2007, 11:41 AM
OK, then use something like this complete replacement for the original code you posted:
<tr>
<?php
$i = 1;
$str = '';
$sql = mysql_query("select * from cat") or die(mysql_error());
while($row = mysql_fetch_array($sql))
{
?>
<td><a href=""><img src="../catpic/<?=$row[filename];?>" height=100 width=100></a></td>
<?php
$str.= ' <td>' . $row['desc'] . ' </td>' . "\n";
if(($i % 3) === 0)
{
echo '</tr><tr>'."\n";
echo $str;
echo '</tr><tr>'."\n";
$str = '';
}
$i++;
}
?>
</tr>
Still needs some adjustment, though -- in the case that you have a number of pictures which are not evenly divisible by three. ;)
raj_2006
07-03-2007, 01:31 PM
Hi
Yes it is working now...but...:)
Frankly speaking can you plz comment the code....as I want to understand what is happening and how.
Also now there are 7 records so it is displaying well...so it will be 3 records in 2 rows and 1 in single...I am not watching any problem there
I need to understand this if condition....PLz post your comments on the lines.
<?php
$str.= ' <td>' . $row['desc'] . ' </td>' . "\n";
if(($i % 3) === 0)
{
echo '</tr><tr>'."\n";
echo $str;
echo '</tr><tr>'."\n";
$str = '';
}
$i++;
}
?>
</tr>
Thanks
raj_2006
07-03-2007, 01:37 PM
Yes you are correct....:D
If it is 7 records then the desc is not appearing for last record.
temp.user123
07-03-2007, 02:03 PM
I've also added the adjustment needed:
<tr>
<?php
$i = 1;
$str = '';
$sql = mysql_query("select * from cat") or die(mysql_error());
while($row = mysql_fetch_array($sql))
{
?>
<td><a href=""><img src="../catpic/<?=$row[filename];?>" height=100 width=100></a></td>
<?php
$str.= ' <td>' . $row['desc'] . ' </td>' . "\n"; # accumulate descriptions
if(($i % 3) === 0) # after three images printed
{ # now print descriptions
echo '</tr><tr>'."\n";
echo $str;
echo '</tr><tr>'."\n";
$str = ''; # clear accumulator
}
$i++; # increment counter
}
while (!empty($str)) # partial row remaining?
{
?>
<td> </td>
<?php
$str.= ' <td> </td>' . "\n"; # fill partial row
if(($i % 3) === 0) # when image row is full
{ # print description row
echo '</tr><tr>'."\n";
echo $str;
$str = ''; # clear accumulator
}
$i++; # increment counter
}
?>
</tr>
raj_2006
07-03-2007, 04:14 PM
Hi
Its fantastic.....excellent sense of logic....:)
Now i need to know how did you think about it....:D and why not its me...
a question:
if(($i % 3) === 0) # after three images printed
{ # now print descriptions
echo '</tr><tr>'."\n";
echo $str;
echo '</tr><tr>'."\n";
$str = ''; # clear accumulator
}
Can you plz explain the below 2 lines.I guess that $str is printing the desc
echo '</tr><tr>'."\n";
echo $str;
echo '</tr><tr>'."\n";
But why this line twice also can i add <td> here
echo '</tr><tr>'."\n";
temp.user123
07-03-2007, 04:23 PM
The td's are in $str -- no need to add anything.
raj_2006
07-04-2007, 01:29 PM
Hi
Its ok now.....I have understood fully...I have experimented with this code a bit more and then I have understood....:D
Thanks a lot mate....Its wonderfull experience here.:)
Raj
temp.user123
07-04-2007, 01:58 PM
You're welcome.