PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
beloveddoll
Forum Commoner
Posts: 40 Joined: Sat Jul 14, 2007 6:18 pm
Post
by beloveddoll » Mon Jul 16, 2007 9:27 am
I have the following code:
Code: Select all
<?
$res = mysql_query("SELECT id, user, item, price FROM coll_inven WHERE user='$log' ORDER BY id ASC");
while($r = mysql_fetch_row($res) ) $comments[] = $r;
if ($comments) foreach($comments as $c){
?>
<?
$res = mysql_query("SELECT id, name, description, url, price, preview FROM collection WHERE id='$c[2]'");
$comico = mysql_fetch_row($res);
mysql_free_result($res);
?>
<a href="collectionview.php?id=<? echo "$comico[0]" ?>"><img src="collection/<? echo "$comico[5]" ?>" border=0></a>
<?
}
?>
How do I tell the code to insert a new paragraph after every 20 entries?
Gente
Forum Contributor
Posts: 252 Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:
Post
by Gente » Mon Jul 16, 2007 9:34 am
Add the $counter, increment it in every iteration and if $counter divides into 20 add paragraph
beloveddoll
Forum Commoner
Posts: 40 Joined: Sat Jul 14, 2007 6:18 pm
Post
by beloveddoll » Mon Jul 16, 2007 9:43 am
Gente wrote: Add the $counter, increment it in every iteration and if $counter divides into 20 add paragraph
Could you make an example? I`m still new the php world. ^^;
Gente
Forum Contributor
Posts: 252 Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:
Post
by Gente » Mon Jul 16, 2007 9:47 am
Code: Select all
$countrer = 0
echo '<p>';
foreach ($a as $b)
{
....
if ($counter % 20 == 0)
{
echo '</p><p>';
}
....
$counter++;
}
echo '</p>'
Something like this...
beloveddoll
Forum Commoner
Posts: 40 Joined: Sat Jul 14, 2007 6:18 pm
Post
by beloveddoll » Mon Jul 16, 2007 10:08 am
Gente wrote: Code: Select all
$countrer = 0
echo '<p>';
foreach ($a as $b)
{
....
if ($counter % 20 == 0)
{
echo '</p><p>';
}
....
$counter++;
}
echo '</p>'
Something like this...
Ok, I want to be sure I understand this.
I would have the code as:
Code: Select all
<?
$countrer = 0
echo '<p>';
foreach ($a as $b)
{
$res = mysql_query("SELECT id, user, item, price FROM coll_inven WHERE user='$log' ORDER BY id ASC");
while($r = mysql_fetch_row($res) ) $comments[] = $r;
if ($comments) foreach($comments as $c){
if ($counter % 20 == 0)
{
echo '</p><p>';
}
?>
<?
$res = mysql_query("SELECT id, name, description, url, price, preview FROM collection WHERE id='$c[2]'");
$comico = mysql_fetch_row($res);
mysql_free_result($res);
?>
<a href="collectionview.php?id=<? echo "$comico[0]" ?>"><img src="collection/<? echo "$comico[5]" ?>" border=0></a>
<?
$counter++;
}
echo '</p>'
?>
Right?
Gente
Forum Contributor
Posts: 252 Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:
Post
by Gente » Mon Jul 16, 2007 10:19 am
No. $a and $b were abstract... I tried to show yours foreach.
BTW. It looks very strange... Every time you execute the same query.
beloveddoll
Forum Commoner
Posts: 40 Joined: Sat Jul 14, 2007 6:18 pm
Post
by beloveddoll » Mon Jul 16, 2007 10:24 am
Gente wrote:
BTW. It looks very strange... Every time you execute the same query.
What do you mean?
This code is to show what a user would have in their inventory if they added anything to their inventory.
beloveddoll
Forum Commoner
Posts: 40 Joined: Sat Jul 14, 2007 6:18 pm
Post
by beloveddoll » Mon Jul 16, 2007 10:26 am
Ok, I tweaked it, does it look the way it should?
Code: Select all
<?
$countrer = 0
echo '<p>';
foreach ($comments as $c)
{
$res = mysql_query("SELECT id, user, item, price FROM coll_inven WHERE user='$log' ORDER BY id ASC");
while($r = mysql_fetch_row($res) ) $comments[] = $r;
if ($counter % 20 == 0)
{
echo '</p><p>';
}
?>
<?
$res = mysql_query("SELECT id, name, description, url, price, preview FROM collection WHERE id='$c[2]'");
$comico = mysql_fetch_row($res);
mysql_free_result($res);
?>
<a href="collectionview.php?id=<? echo "$comico[0]" ?>"><img src="collection/<? echo "$comico[5]" ?>" border=0></a>
<?
$counter++;
}
echo '</p>'
?>
Gente
Forum Contributor
Posts: 252 Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:
Post
by Gente » Mon Jul 16, 2007 10:31 am
Looks good but please but that's would be better:
Code: Select all
<?
$countrer = 0
echo '<p>';
foreach ($comments as $c)
{
$res = mysql_query("SELECT id, user, item, price FROM coll_inven WHERE user='$log' ORDER BY id ASC");
while($r = mysql_fetch_row($res) ) $comments[] = $r;
?>
<?
$res = mysql_query("SELECT id, name, description, url, price, preview FROM collection WHERE id='$c[2]'");
$comico = mysql_fetch_row($res);
mysql_free_result($res);
?>
<a href="collectionview.php?id=<? echo "$comico[0]" ?>"><img src="collection/<? echo "$comico[5]" ?>" border=0></a>
<?
$counter++;
if ($counter % 20 == 0)
{
echo '</p><p>';
}
}
echo '</p>'
?>
beloveddoll
Forum Commoner
Posts: 40 Joined: Sat Jul 14, 2007 6:18 pm
Post
by beloveddoll » Mon Jul 16, 2007 12:44 pm
I get this error:
Parse error: syntax error, unexpected T_ECHO in /home/secrett1/public_html/testing/collection.php on line 36
ianhull
Forum Contributor
Posts: 310 Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK
Post
by ianhull » Mon Jul 16, 2007 12:52 pm
whoops
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Mon Jul 16, 2007 1:13 pm
This will give you problems for more than one reason...
beloveddoll
Forum Commoner
Posts: 40 Joined: Sat Jul 14, 2007 6:18 pm
Post
by beloveddoll » Mon Jul 16, 2007 2:13 pm
I fixed that and it still gives me this error message:
Parse error: syntax error, unexpected T_ECHO in /home/secrett1/public_html/testing/collection.php on line 36
line 36 is:
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Mon Jul 16, 2007 2:14 pm
Show us the fix you implemented.
beloveddoll
Forum Commoner
Posts: 40 Joined: Sat Jul 14, 2007 6:18 pm
Post
by beloveddoll » Mon Jul 16, 2007 2:17 pm
Code: Select all
<?
$counter = 0
echo "<p>";
foreach ($comments as $c)
{
$res = mysql_query("SELECT id, user, item, price FROM coll_inven WHERE user='$log' ORDER BY id ASC");
while($r = mysql_fetch_row($res) ) $comments[] = $r;
?>
<?
$res = mysql_query("SELECT id, name, description, url, price, preview FROM collection WHERE id='$c[2]'");
$comico = mysql_fetch_row($res);
mysql_free_result($res);
?>
<a href="collectionview.php?id=<? echo "$comico[0]" ?>"><img src="collection/<? echo "$comico[5]" ?>" border=0></a>
<?
$counter++;
if ($counter % 20 == 0)
{
echo '</p><p>';
}
}
echo '</p>'
?>