New paragraph for every 20 entries

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

New paragraph for every 20 entries

Post by beloveddoll »

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?
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

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 »

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. ^^;
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

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 »

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?
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

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 »

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 »

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>'
?>
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

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 »

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 »

whoops
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

This will give you problems for more than one reason...

Code: Select all

<?php
$countrer = 0
?>
beloveddoll
Forum Commoner
Posts: 40
Joined: Sat Jul 14, 2007 6:18 pm

Post by beloveddoll »

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:

Code: Select all

echo "<p>";
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Show us the fix you implemented.
beloveddoll
Forum Commoner
Posts: 40
Joined: Sat Jul 14, 2007 6:18 pm

Post by beloveddoll »

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>'
?>
Post Reply