Putting thisPHP code into 1 table

Discussion in 'PHP' started by clouting, Dec 21, 2007.

  1. #1
    Hi

    i have made this code to display the most popular games on my site.

    <?
    ##
    # AV ARCADE v3
    # popular.php
    # Loads the most popular games/media module
    ##
    $sql = mysql_query("SELECT * FROM ava_games WHERE published=1 ORDER BY hits desc LIMIT 10");
    	while($row = mysql_fetch_array($sql))
    	{
    		$abcd= $row['name'];
    		$abcd = str_replace (" ", "-", $abcd);
    			
    			if ($seo_on == 0) 
    			{
    				$url = 'games/images'.$row['id'].'';
    			}
    			else 
    			{
    				$url = 'view/'.$row['image'].'/'.$abcd.'.png';
    			}
    		
    
    ?>
    <table border = 1>
    	<tr>
    		<td>
    			<? echo '&nbsp;<a href="'.$site_url.'/'.$url.'">'.$row['name'].'</a><br>'; ?>
    
    		</td>
    	</tr>
    	<tr>
    		<td>
    			<? echo '<img src="'.$row['image'].'" width="'.$image_width.'" height="'.$image_height.'" alt="" />'; ?>
    		</td>
    	</tr>
    
    </table>
    
    <?
    	}
    ?>
    
    Code (markup):
    however the output displays 10 results all in separate tables...I want it to display all results in the same table, with the images all in line on one row and the title underneath the relevant thumbnail..

    any ideas???

    i would appreciate your help as am a bit of a novice with PHP
     
    clouting, Dec 21, 2007 IP
  2. justfred

    justfred Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    no big deal that's pretty good work, you didn't end your while loop.

    EDIT: the <table> needs to be outside the while loop.
     
    justfred, Dec 21, 2007 IP
  3. justfred

    justfred Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    something more like:
    <table border = 1>
    <?
    $sql = mysql_query("SELECT * FROM ava_games WHERE published=1 ORDER BY hits desc LIMIT 10");
    	while($row = mysql_fetch_array($sql))
    	{
    		$abcd= $row['name'];
    		$abcd = str_replace (" ", "-", $abcd);
    			
    			if ($seo_on == 0) 
    			{
    				$url = 'games/images'.$row['id'].'';
    			}
    			else 
    			{
    				$url = 'view/'.$row['image'].'/'.$abcd.'.png';
    			}
    		
    
    ?>
    	<tr>
    		<td>
    			<? echo '&nbsp;<a href="'.$site_url.'/'.$url.'">'.$row['name'].'</a><br>'; ?>
    
    		</td>
    	</tr>
    	<tr>
    		<td>
    			<? echo '<img src="'.$row['image'].'" width="'.$image_width.'" height="'.$image_height.'" alt="" />'; ?>
    		</td>
    	</tr>
    
    <?
    	}
    ?>
    </table>
    PHP:
    the difference, as I mentioned above, is that the table is moved outside the loop.
     
    justfred, Dec 21, 2007 IP
  4. justfred

    justfred Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    oh and if you want it on one row, take the <tr> tags out of the loop too.
     
    justfred, Dec 21, 2007 IP
  5. clouting

    clouting Guest

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks justfred

    but
    did youmean to modify thecode like this

    the stops the php from working.
     
    clouting, Dec 21, 2007 IP
  6. clouting

    clouting Guest

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    sorry just read the other posts didnt realise they were there.

    thanks for your help
     
    clouting, Dec 21, 2007 IP
  7. clouting

    clouting Guest

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thanks a lot justfred

    that works fine
     
    clouting, Dec 21, 2007 IP
  8. justfred

    justfred Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    cool, glad to hear it. I wasn't 100% sure myself, but I thought it should work. Still may need some work if you copy and pasted my code, it was meant to be an example.

    well have a good holiday ;)

    EDIT: you may also try a nested table, that might give you more flexibility in your html
     
    justfred, Dec 21, 2007 IP