SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 35
  1. #1
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Showing a # of results per line?

    I want to show 4 results from my table and then on the next line show 4 more, and so on.

    I'm pretty sure there was an easy way to do this, but I can't think of it. So I came to ask the experts, please help.

  2. #2
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    695
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    That's not an SQL issue. Handle it in the presentation layer. I.e. retrieve the data as normal and when you have 4 records or come to the end, write those records. There may be other better solutions but that depends in which environment your presentation is coded.

  3. #3
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using PHP, but I am very new to it. I can use LIMIT to show 4 results, as I want 4 per line, but then I don't get a 2nd line. I could code this with lots of if's and ifelse's, but I know thats not the right way. Could you head me in the direction of what to do?

  4. #4
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    define ('ROWS_PER_GROUP'4);

    $result mysql_query('select field_list from table_name order by id'$connection_id);

    $i 0;
    $groups = array();

    while (
    $row mysql_fetch_assoc($result))
    {
      
    $groups[$i % ROWS_PER_GROUP] = $row;
      
    $i++;

    EDIT: use percent sign instead of %

  5. #5
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used that but I don't see how that is supposed to make there be 4 per row, and it doesn't. Also, where is $connection_id defined, do I need it? I had to change "%" to a % because of a parse error.

    Where do I throw in my html that I want to display? I am using a table so I need to echo a </tr> and a <tr> after each 4. If I put it within the while{} than I see all the results on one line, not just 4.

    I'm sorry, but I am new to this so I'm having trouble finding out how to make this work.

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,015
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    thread moved to php forum
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    What you want is pagination.
    http://www.google.com/search?q=PHP+pagination
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you get by using:

    PHP Code:
    print_r($groups); 

  9. #9
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by earl-grey View Post
    What do you get by using:

    PHP Code:
    print_r($groups); 
    I get a lot of Array info, see here:
    http://www.gameanyone.com/?p=game&ga...%20Duty&view=2

    Below that I have my normal echo which is showing all the results on 1 line.

  10. #10
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What exactly do you want to output?

  11. #11
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to output a table <td></td> which includes an image, and a link title which are both in the database. I want to show 4 results, and then have the next 4 on the next line, and so on. To do this I will need to echo a "</tr>" after every 4th result and somewhere throw in <tr>, I just don't know how to do this.

  12. #12
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will write pseudo-code, so you can study by writing code yourself

    Code:
    output "<table>"
    for each group
    {
      output "<tr>"
      output "<td>"
      for each row in current group
      {
        output row details
      }
      output "</td>"
      output "</tr>"
    }
    output "</table>"

  13. #13
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't automagically get markup from the array, the idea there is to put together an array where each array element is an array of four database results. You loop through the master loop and implode the 4 inner ones into table cells. There may be a better way to do this, but that's what earl-grey's code is trying to do.

  14. #14
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't tested this, but it should do what you want it to do.

    PHP Code:
    $tbl = array();
    $count 1;
    $r = array();
    $table '<table>[content]</table>';
    while(
    $row mysql_fetch_assoc($result))
    {
        
    $r[] = $row;
        if(
    $count 5)
        {
            
    $count 0;
            
    $tbl[] = '<td>'.implode('</td><td>',$r).'</td>';
            
    $r = array();
        }
        
    $count++;
    }
    echo(
    str_replace('[content]','<tr>'.implode('</tr><tr>',$tbl)).'</tr>',$table); 
    That is a lot to put in one row however, and it might be hard for a user to scan in a natural fashion.

  15. #15
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm starting to get it, but Earl has to be a meanie

    I don't know what to put for the foreach's, I looked a lot on php.net but its all over my head. Could someone tell me what to use in the foreach()'s if I am using earl-grey's code from before.

    Code:
    foreach(group as $groups){
    
    	echo "<tr>";
    
    foreach(row){
    
    
             echo "
             <td class='4' align='center' valign='center' height='150' width='150'>
             <a href='http://www.gameanyone.com?p=video&game=$game&video=".$row['VTitle']."'><img src='".$row['IFilename']."' border='0' height='100'><br>".$row['VTitle']."
             </a></td>
            ";
    
    }
    
    echo "</tr>";
    
    }
    Edit: I was posting at the same time as Hammer65, I will try it.

  16. #16
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hammer65 View Post
    I haven't tested this, but it should do what you want it to do.

    PHP Code:
    $tbl = array();
    $count 1;
    $r = array();
    $table '<table>[content]</table>';
    while(
    $row mysql_fetch_assoc($result))
    {
        
    $r[] = $row;
        if(
    $count 5)
        {
            
    $count 0;
            
    $tbl[] = '<td>'.implode('</td><td>',$r).'</td>';
            
    $r = array();
        }
        
    $count++;
    }
    echo(
    str_replace('[content]','<tr>'.implode('</tr><tr>',$tbl)).'</tr>',$table); 
    That is a lot to put in one row however, and it might be hard for a user to scan in a natural fashion.
    Hey, you should've let jetnine do it him/herself

  17. #17
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I'm still stuck, I tried Hammer65's way and I just kept getting errors. earl-grey's way is more on my level and at least makes sense to me; not as complicated. I would appreciate it if someone would explain what to use for the foreach()'s.

  18. #18
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jetnine View Post
    Well I'm still stuck, I tried Hammer65's way and I just kept getting errors. earl-grey's way is more on my level and at least makes sense to me; not as complicated. I would appreciate it if someone would explain what to use for the foreach()'s.
    You have to supply at least two "arguments" to a foreach construct: array to iterate over and variable where array values will be stored, one at a time.

    What we have is a $groups array, each element of which an array of rows.

    In first foreach you'd have $groups as $rows, and in the second $rows as $row.

  19. #19
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At least its going down to the next line now, but for some reason theres 8 per line even if there aren't 8 results. And its not showing the results correctly, its just showing random numbers and letters where the video titles are supposed to be, same with the image path.

    You can see what its doing now here: http://www.gameanyone.com/?p=game&game=Gun&view=2

    I've never used this 'define' before, is that the problem?

    PHP Code:
    define ('ROWS_PER_GROUP'4);

            
    $result mysql_query("SELECT * FROM videos WHERE GTitle = '$game' AND VTitle NOT LIKE '%</a>%' ORDER BY sort")
                      or die(
    mysql_error());
    $i 0;
    $groups = array();

    while(
    $row mysql_fetch_assoc($result)){

      
    $groups[$i ROWS_PER_GROUP] = $row;

      
    $i++;

    foreach(
    $groups as $rows){

        echo 
    "<tr>";

    foreach(
    $rows as $row){


             echo 
    "
             <td class='4' align='center' valign='center' height='150' width='150'>
             <a href='http://www.gameanyone.com?p=video&game=
    $game&video=".$row['VTitle']."'><img src='".$row['IFilename']."' border='0' height='100'><br>".$row['VTitle']."
             </a></td>
            "
    ;

    }

    echo 
    "</tr>";



  20. #20
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take the foreach's out of of the while

    PHP Code:
    define ('name'value); 
    declares constant, like = operator is used for assigning a value to a variable :

    PHP Code:
    $name value

  21. #21
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have moved the following around to a few places with no luck. Shouldn't it stay in the while to get the row data? Am I moving something I shouldn't be, or not moving something that I should?

    PHP Code:
    foreach($groups as $rows){

        echo 
    "<tr>";

    foreach(
    $rows as $row){


             echo 
    "
             <td class='4' align='center' valign='center' height='150' width='150'>
             <a href='http://www.gameanyone.com?p=video&game=
    $game&video=".$row['VTitle']."'><img src='".$row['IFilename']."' border='0' height='100'><br>".$row['VTitle']."
             </a></td>
            "
    ;

    }

    echo 
    "</tr>";



  22. #22
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    foreach's are fine, could you post the whole file?

    They shouldn't be in while, as in while we make the array, and after that (when it is made), we iterate over it.

  23. #23
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here it is. I have put the foreach's back where I had them before.

    PHP Code:
                 echo "<table border='0' cellspacing='0' cellpadding='1' width='$width'>
                        <tr>
                             <th colspan='2'>Videos</th>
                        </tr>"
    ;
    define ('ROWS_PER_GROUP'4);

            
    $result mysql_query("SELECT * FROM videos WHERE GTitle = '$game' AND VTitle NOT LIKE '%</a>%' ORDER BY sort")
                      or die(
    mysql_error());

    $i 0;

    $groups = array();


    while(
    $row mysql_fetch_assoc($result)){


    $image5=$row['IFilename'];
       if (
    $image5==""){
    $image6="images/favicon.gif";
    }else{
    $image6=$row['IFilename'];
    }

      
    $groups[$i ROWS_PER_GROUP] = $row;

      
    $i++;


    foreach(
    $groups as $rows){

        echo 
    "<tr>";

    foreach(
    $rows as $row){


             echo 
    "
             <td class='4' align='center' valign='center' height='150' width='150'>
             <a href='http://www.gameanyone.com?p=video&game=
    $game&video=".$row['VTitle']."'><img src='".$row['IFilename']."' border='0' height='100'><br>".$row['VTitle']."
             </a></td>
            "
    ;

    }

    echo 
    "</tr>";

    }


    }

            echo 
    "</table>";



  24. #24
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Number of of opening and closing brackets are different. Is code executed in another loop?

  25. #25
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the last } is for the 'view if' which is irrelevant for this. It tells it to instead of show this: http://www.gameanyone.com/?p=game&ga...#37;20Ignition
    show this: http://www.gameanyone.com/?p=game&ga...gnition&view=2

    I didn't put up the whole file as it is quite long.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •