SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    somebody tidy up my php for heavens sake!!

    Hi

    I've struggled, puzzled and guessed and finally got the specific info I need from an array obtained from a query but it looks 'round the houses' to me.

    Can anybody make this more consice?

    PHP Code:
    $query "SELECT cid, name, introtext   
                           FROM categories
                           WHERE categorytype='parent'"
    ;
    $resultmysql_db_query("my_db1"$query);    
    while (
    $r[0]= mysql_fetch_array($result)) {
    //echo var_dump($r[0]); 
    $cid =  $r[0]['cid'];
    $name $r[0]['name'];
    echo 
    $cid;
    echo 
    $name;
    }

    while (
    $r[1]= mysql_fetch_array($result)) {
    //echo var_dump($r[1]); 
    $cid =  $r[1]['cid'];
    $name $r[1]['name'];
    echo 
    $cid;
    echo 
    $name;
    }

    while (
    $r[2]= mysql_fetch_array($result)) {
    //echo var_dump($r[2]); 
    $cid =  $r[2]['cid'];
    $name $r[2]['name'];
    echo 
    $cid;
    echo 
    $name;
        } 

  2. #2
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not 100% sure what your trying to do, but heres my try:

    PHP Code:
    <?php
    // you should have connected to the database already (i assume you have)
    // if you havent, put the database connection in here

    $query "SELECT cid, name, introtext
            FROM categories
            WHERE categorytype='parent'"
    ;
    $result mysql_query($query) or die (mysql_error());    

    // take every row from the query and show their feild "cid" and "name"
    while ($row mysql_fetch_array($result)) {

        echo 
    $row['cid'].' - '$row['name'];

    }

    ?>
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  3. #3
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats tidier thanks but here's what i want to do with it....

    all this info is printed out in a while statement which is like a list but I would like to 'label' each rows items and have more flexibility in the way they are displayed

    I.E.

    row 0 > cid = $cid0
    row 0 > name = $name0

    row 1 > cid = $cid1
    row 1 > name = $name1

    row 2 > cid = $cid2
    row 2 > name = $name2

    and then drop the new vars $name0, $name1, $name2 etc in wherever i want on my page.

    does that make any sense?

  4. #4
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mean like this?

    PHP Code:

    $i 
    0;

    while (
    $row mysql_fetch_array($result)) {

        echo 
    'row '.$i.' > cid = '.$row['cid'].'<br />';
        echo 
    'row '.$i.' > name = '.$row['name'].'<br /><br />';
        
    $i++;


    Last edited by AlienDev; Jul 20, 2007 at 07:31. Reason: typo
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  5. #5
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ummm no. you've taken me too literally.

    how do i assign a $name to each of the values so that I can print them outside of the 'while' statement.

    EG

    so i can the print the values from first row

    then have some html

    then print the values from second row

    then have an image and whatever else i like

    then print the values from third row.

    Is it easier to just run 3 queries on the db - each one asking for the specific row.when i started out that it would be ugly to run 3 queries on the same table just to pick out 3 induvidual rows. Now I'm not so sure.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If you want to "reuse" a set of mysql results without going back to the database you could use mysql_data_seek() www.php.net/mysql_data_seek

    If you dont want to do that then assign the results to another array as you loop through the results, but that seems a waste of resources to me.


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
  •