Jump to content


Photo

help.. modulo?


  • Please log in to reply
5 replies to this topic

#1 Tkaspers88

Tkaspers88
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 02 August 2006 - 01:20 PM

I have a database with information about MMORPG monsters.. now I want to create a creatureoverview like you can see here: http://www.tibia.com...topic=creatures and here http://www.tibianews.net/bestiary.asp etc..

I want to show 5 creatures on a row..
I use this code to get the information from the database and to show them:

<?php

include('../dbconfig.php');

$sql = "SELECT ImageAdress,NameUrl,Name,Experience,Hitpoints FROM Creatures ORDER BY Name";
if(!$result = mysql_query($sql))
{
  echo "Error: Creating creaturelist failed. Please try again!";
}
else
{
  if(mysql_num_rows($result) == 0)
  {
    echo "Error: No results.. Please try again!";
  }
  else
  {
    while($row = mysql_fetch_assoc($result))
    {
      echo 
	  
	  
'<TD><A HREF="creature.php?xd='.$row['NameUrl'].'"><IMG border=0 src="images/'.$row['ImageAdress'].'" width=64 height=64>
</A><BR><font size="1" face="Verdana, Arial, Helvetica, sans-serif" color="#CCCCCC"><strong>'.$row['Name'].'</strong></font>
<br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif" color="#999999"><em>Exp: '.$row['Experience'].'</em></font>
<br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif" color="#FFFFFF"><em>HP: '.$row['Hitpoints'].'</em></font></TD>';
	  
	  
    }
  }
}

?>

But it shows them wrong... As you can see it shows the image,name,exp and hp and it retrieves all creatures from my database.. how can i make it like i want it?
I want it to show 5 creatures next to each other and not all of them.. so it needs to start a new row after 5 creatures..

Maybe its modulo?


//Thijs

#2 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 02 August 2006 - 01:56 PM

Something like this perhaps?

    $i=1;
   while($row = mysql_fetch_assoc($result))
    {
	  if ($i == 1)
        echo '<tr>';
	  
      echo	  
'<TD><A HREF="creature.php?xd='.$row['NameUrl'].'"><IMG border=0 src="images/'.$row['ImageAdress'].'" width=64 height=64>
</A><BR><font size="1" face="Verdana, Arial, Helvetica, sans-serif" color="#CCCCCC"><strong>'.$row['Name'].'</strong></font>
<br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif" color="#999999"><em>Exp: '.$row['Experience'].'</em></font>
<br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif" color="#FFFFFF"><em>HP: '.$row['Hitpoints'].'</em></font></TD>';
	  
	  if ($i == 5)
	    echo '</tr>';
	  $i++;
	  
    }
    if ($i != 1 && $i != 5)
      echo '</tr>';
    echo '</table>';


Ronald  ;D
RTFM is an almost extinct art form, it should be subsidized.

#3 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 02 August 2006 - 02:27 PM

almost perfect, but where do I define my variables retrieved from the db?
http://www.DaveLinger.com
dave at linger dot com

#4 Tkaspers88

Tkaspers88
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 02 August 2006 - 02:56 PM

Something like this perhaps?

    $i=1;
   while($row = mysql_fetch_assoc($result))
    {
	  if ($i == 1)
        echo '<tr>';
	  
      echo	  
'<TD><A HREF="creature.php?xd='.$row['NameUrl'].'"><IMG border=0 src="images/'.$row['ImageAdress'].'" width=64 height=64>
</A><BR><font size="1" face="Verdana, Arial, Helvetica, sans-serif" color="#CCCCCC"><strong>'.$row['Name'].'</strong></font>
<br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif" color="#999999"><em>Exp: '.$row['Experience'].'</em></font>
<br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif" color="#FFFFFF"><em>HP: '.$row['Hitpoints'].'</em></font></TD>';
	  
	  if ($i == 5)
	    echo '</tr>';
	  $i++;
	  
    }
    if ($i != 1 && $i != 5)
      echo '</tr>';
    echo '</table>';


Ronald  ;D



It works "almost".. It works for the first 5 creatures.. but all the others are next to each other.. hmm

Greetings,
Thijs

#5 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 02 August 2006 - 03:08 PM

Sorry, overlooked the obvious reset of $i. Should be:

if ($i == 5) {
   echo '</tr>';
   $i=1;
}     
else 
   $i++;

Ronald  ;D
RTFM is an almost extinct art form, it should be subsidized.

#6 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 02 August 2006 - 04:43 PM

if($i%5 == 0)

then you don't even need to reset.

so, modulo is the answer :)
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users