#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    77
    Rep Power
    15
    Hi,

    Can someone tell me if it is possible to split data that is returned from a SELECT statement into two columns. I want to fit more data on the page so this would really help. I was thinking of somehow splitting the array. Is this possible and if so, how? Thanks in advance.

    Dave.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 1999
    Posts
    114
    Rep Power
    16
    Hi,

    Could you give a more specific example of what you're trying to do?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    77
    Rep Power
    15
    Well I have a phonebook system that has all contacts of different staff members in it. I want to search say for a particular location or department. So the select statement will be embedded into the statement... (eg. SELECT * from staff where dept = "IT") But I want this data to be displayed in two columns. The main reason is because some people wish to be able to print the results out. So basically the select statment isn't the problem, its just a matter of how do I display the results returned in two (or more columns)?? I want the data to run down one column, then when that fills up it should start the next column. Is this possible? Thanks for your help.

    Dave
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    well assuming you get an array from your query $result - holding say 50 rows, why not try something like;

    <select name="firsttwenty">
    <?php for($x=0;$x=19;$x++){ ?>
    <option value=<?php echo $result[$x];?>><?php echo $result[$x];?>
    <?php }?>
    </select>

    <select name="secondtwenty">
    <?php for($x=19;$x=39;$x++){ ?>
    <option value=<?php echo $result[$x];?>><?php echo $result[$x];?>
    <?php }?>
    </select>


    you are better off using a while loop - or at least counting the result first - but you get the idea.

    Simon.

    ------------------
    Simon Wheeler
    FirePages -DHTML/PHP/MySQL
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    Sorry - got the wrong end of the stick I think (select on the brain)

    you can do the same thing to a table though ie:

    <table><tr>
    <td>
    <?php for($x=0;$x=19;$x++){ echo "$result[$x] <br>";} ?>
    </td>
    <td>
    <?php for($x=19;$x=39;$x++){ echo "$result[$x] <br>";} ?>
    </td>
    </tr></table>

    (the <br> saves work)

    same again a while loop would be better unless you know the exact no of rows returned.

    you can use array functions like array_walk/values , explode etc but if you just need to sort out the printing problem this is probably as hard as it need get.

    Regards,
    Simon.


    ------------------
    Simon Wheeler
    FirePages -DHTML/PHP/MySQL
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    77
    Rep Power
    15
    Hi, I tried the code you suggested and it seemed quite ok to me but I am having trouble with it. At first (after a huge amount of processing) it returned an error about $result not being an array. However, I do use result in my script. Here it is here:

    $result = mysql_query ("SELECT * FROM staff
    WHERE name LIKE '%$name%'
    ORDER BY name
    LIMIT 500
    ");

    Now when I run the script it just crashes my browser. Do you know what could be wrong? Also, if say i didn't know how many records were to be returned could I say for (x=0; x=mysql_numrows($result)/2; x++). Just an idea, but then I am wondering how I would get the second set of records ?? Thanks again for your help.

    Dave
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Alkmaar, The Netherlands
    Posts
    123
    Rep Power
    15
    After you executed the query you can use mysql_num_rows($result) to get the number of rows. Then you can use mysql_result($result,$i,"fieldname") to display the result in "fieldname" and row $i.

    Your code would then look something like this:

    $result = mysql_query ("SELECT * FROM staff WHERE name LIKE '%$name%' ORDER BY name LIMIT 500 ");

    $rows = mysql_num_rows($result);
    x = $rows / 2;
    //To split up the result in 2 columns

    echo "<table>";
    for($i=0;$i=x;$i++)
    {
    echo "<tr><td>";
    echo mysql_result($result,$i,"fieldname");
    echo "</td><td>";
    echo mysql_result($result,$i+x,"fieldname");
    echo "</td></tr>";
    }

    if (($rows % 2)==1)
    {
    //There is one result/row left
    echo "<tr><td>";
    echo mysql_result($result,($rows-1),"fieldname");
    //$rows-1 because the result-set is 0-based.
    echo "</td><td>&nbsp;</td></tr>";
    }
    echo "</table>";



    ------------------
    Good luck,
    Bas

    ------------------
    E-mail me at: b.vandermeijden@pecoma.nl
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    77
    Rep Power
    15
    Just two questions... the first is, with the "fieldname" you reference... do i need to change this to say $name, $phone etc??

    Also when leaving x as it is I get a parse error. Is it correct to name it as $x or does this change its scope?? Thanks.

    Dave
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    77
    Rep Power
    15
    At the moment I am getting the error... "Warning: Unable to jump to row 2 on MySQL result index 2" using this code. I think i also have a problem with my loop. You see I previously had it setup so that I had one record per row... now I want that day to be in two columns. The loops job was to keep going to the end of the search results were found. Do I still need it with this for loop?? Anyway here is my code...

    if ($row = mysql_fetch_array($result))
    {
    do {

    $resultsnumber = mysql_numrows($result);

    $x = $resultsnumber / 2;
    //To split up the result in 2 columns

    echo "<table border='1' width='100%'>";
    for ($i=0;$i=$x;$i++)
    {
    echo "<tr><td>";
    echo mysql_result($result,$i,name);
    echo "</td><td>";
    echo mysql_result($result,$i+$x,location);
    echo "</td><td>";
    echo mysql_result($result,$i+$x,phone_extension);
    echo "</td></tr>";
    }

    if (($resultsnumber % 2)==1)
    {
    //There is one result/row left
    echo "<tr><td>";
    echo mysql_result($result,($rows-1),name);
    //$rows-1 because the result-set is 0-based.
    echo "</td><td> </td></tr>";
    }
    echo "</table>";

    } while($row = mysql_fetch_array($result));
    }
    }

    Please help, this is sending me insane!!! Thanks

    Dave
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Alkmaar, The Netherlands
    Posts
    123
    Rep Power
    15
    Just two questions... the first is, with the "fieldname" you reference... do i need to change this to say $name, $phone etc??

    No, you should use the columnnames from mySQL instead. Make sure you pass them as strings.

    Also when leaving x as it is I get a parse error. Is it correct to name it as $x or does this change its scope??

    Did you change the little error I had posted?
    In line 4 of the code I forgot the $ sign in front of the x. so it is supposed to be: $x = $rows/2;


    ------------------
    Good luck,
    Bas

    ------------------
    E-mail me at: b.vandermeijden@pecoma.nl
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Alkmaar, The Netherlands
    Posts
    123
    Rep Power
    15
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by mouserdj:
    At the moment I am getting the error... "Warning: Unable to jump to row 2 on MySQL result index 2" using this code. I think i also have a problem with my loop. You see I previously had it setup so that I had one record per row... now I want that day to be in two columns. The loops job was to keep going to the end of the search results were found. Do I still need it with this for loop?? Anyway here is my code...

    Please help, this is sending me insane!!! Thanks

    Dave
    [/quote]

    Did you see any results before the error occured (eg did you see the opening tag of the table in your source) or didn't you see anything?

    Please try the before first. For faster answer e-mail me. I'll be leaving for holidays next week and won't be back for 3 weeks.



    ------------------
    Good luck,
    Bas

    ------------------
    E-mail me at: b.vandermeijden@pecoma.nl
  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    2
    Rep Power
    0
    This is a script to make two or even more columns.

    Enjoy,

    Bjorn


    $result=mysql_query("SELECT ArtikelNaam FROM Artikel");
    $NumRows=mysql_num_rows($result);

    /* You got to do this result minus 1 cause mysql starts counting from 0)
    This 1 will be used later again. A little further in the script*/
    $x=($NumRows/2)-1;

    if ($NumRows%2==1){
    $x=$x+0.5;
    }

    echo "<table cellpadding=4 cellspacing=4>";

    for ($i=0;$i<=$x;$i++){
    /* This is to find out if the last row contains 1 or 2 records. If it has only 1 record left, the right side will give an error. So when there will be output from the last record, it will check if the output will be odd or not.*/
    if ($i==$x && $NumRows%2==1){
    echo "<tr><td width='50%'>";
    echo mysql_result($result,$i,"ArtikelNaam")
    echo "</td><td width='50%'>";
    echo "&nbsp";
    echo "</td>";
    }
    else
    {
    echo "<tr><td width='50%'>";
    echo mysql_result($result,$i+$x+1,"ArtikelNaam")
    echo "</td><td width='50%'>";
    echo mysql_result($result,$i+$x+1,"ArtikelNaam")
    echo "</td>";
    }
    }

    echo "</table>";
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    Sorry Guys there is a way easier way to accomplish this!
    $result=mysql_query("SELECT ArtikelNaam FROM Artikel");

    //Set this to the number of columns you want
    $cols = 2;
    $i = 1;
    echo "<table cellpadding=4 cellspacing=4><tr>";
    while ($row = mysql_fetch_array($result)) {
    if (is_int($i / $cols)){
    echo "<td width='50%'>";
    echo $row["ArtikelNaam"];
    echo "</td></tr><tr>";
    }
    else {
    echo "<td width='50%'>";
    echo $row["ArtikelNaam"];
    echo "</td>";
    }
    $i++;
    }

    echo "</tr></table>";

Similar Threads

  1. Replies: 3
    Last Post: February 13th, 2004, 12:04 AM
  2. Codepupil
    By Adachic in forum C Programming
    Replies: 3
    Last Post: February 6th, 2004, 07:50 PM
  3. post data to new window then do redirect
    By theDano in forum PHP Development
    Replies: 2
    Last Post: January 24th, 2004, 07:08 PM
  4. my comp sux lately , need help from pro's
    By Hykinsel007 in forum Windows Help
    Replies: 2
    Last Post: January 5th, 2004, 02:52 PM
  5. MySQL how do you get columns of data out??
    By byte me in forum PHP Development
    Replies: 5
    Last Post: January 3rd, 2004, 02:30 PM

IMN logo majestic logo threadwatch logo seochat tools logo