SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: Reusing A Query

  1. #1
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone have any idea of how to reuse a query on the same page?

    For example:

    $query_listimages = "SELECT imagedescription FROM tbl_image_library ORDER BY imagedescription";

    $result_listimages = mysql_query($query_listimages);

    while($row = mysql_fetch_array($result_listimages)) {
    $imageID = $row["imageID"];
    $imagedescription = $row["imagedescription"];
    ...yadda, yadda, yadda...

    On my page this will generate a drop down menu. But I need to reuse this same query 2 more times. How can I do this without making 2 more queries to the database?
    <///////~

    www.sohh.com

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2000
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    stop giving $result_listimages a new vaule each time you use it and use different varible name ??





    Need a CGI Script ?? checkout CGI-Scripting

  3. #3
    SitePoint Evangelist
    Join Date
    May 2000
    Location
    Canada
    Posts
    533
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_data_seek($result_listimages,0); is what you want
    cogito, ergo sum

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually I think what you want to do is store the results of the query in an array and then use the array over and over again. For example

    $query_listimages = "SELECT imagedescription FROM tbl_image_library ORDER BY imagedescription";
    $result_listimages = mysql_query($query_listimages);
    while($row = mysql_fetch_array($result_listimages)) {
    $data[] = array("imageID" => $row["imageID"],
    "imageDescription" => $row["imagedescription"]
    );


    So now all your records are in an array and you can reuse this array

    Use the first time(print all the image ids)
    while(list($key,$val) = each ($data)) {
    print $data[$key]["imageID"];
    }

    A little further down the page you want to print the ids again or the ids and the descriptions
    reset($data);
    print '<select>';
    while(list($key,$val) = each ($data)) {
    printf('<option value="%s">%s', $data[$key]["imageID"], $data[$key]["imageDescription"]);
    }
    print '</select>';



    Maybe one more time
    reset($data);
    while(list($key,$val) = each ($data)) {
    print $data[$key]["imageDescription"];
    }

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Once again, I don't think I've explained it properly. So here is a link to what I'm trying to do:

    http://www.sohh.com/pub2000

    username and password: ue

    Once you're logged in go to:
    http://www.sohh.com/pub2000/content_add.php

    You'll see an red dotted line area. In there is segment called "Related Image for Page 1:" Notice how long that drop down menu is. I am trying to repeat this same query for "Related Image for Page 2" and "Related Image for Page 3".

    If someone else can come up with a better way for me to assign images to my storys (outside of hard coding the images) that would be great too.
    <///////~

    www.sohh.com

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what is the problem with my code you should run the query above everything and create the array like I showed then simply loop through the array to create each drop down then reset the array to the start and use it again, can we see the php code you used to try and create the 1st, 2nd and third drop downs?

    Don't think of it as repeating the query, think of it as reusing the data from the query, I use this method all the imte for this very thing, I know it works, so show us the code you used so we can see why it is not working for you.
    <Edited by freddydoesphp on 01-31-2001 at 01:38 PM>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, I was writing my message before you came up with the code...no big deal...I'm trying your method now...brb
    <///////~

    www.sohh.com

  8. #8
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Freddy,

    For some reason, this is working out for me. So far I've got:

    $query_listimages = "SELECT tbl_image_library.imagedescription, tbl_image_library.imageID FROM tbl_image_library ORDER BY imagedescription";

    $result_listimages = mysql_query($query_listimages);

    if (!$result_listimages) {
    echo "Error performing query: " . mysql_error();
    exit;
    }

    while($row = mysql_fetch_array($result_listimages)) {
    $data[] = array("imageID" => $row["imageID"], "imagedescription" => $row["imagedescription"]);
    }

    Where I want the first drop down box to appear, I have:

    while(list($key,$val) = each ($data)) {
    print $data[$key]["imageID"];
    }

    which spits all of the IDs in one dropdown selection. You'd have to see it for yourself.

    So this is where I'm stuck.

    If you want I could send you the file via e-mail.

    Thanks
    <///////~

    www.sohh.com

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what is the problem, didn't you say you wanted to be able to create the same dropdown three times on one page?

    while(list($key,$val) = each ($data)) {
    print $data[$key]["imageID"];
    }

    reset($data);
    while(list($key,$val) = each ($data)) {
    print $data[$key]["imageID"];
    }

    reset($data);
    while(list($key,$val) = each ($data)) {
    print $data[$key]["imageID"];
    }


    Would print the IDS list three times.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Argh!

    My bad Freddy.

    I meant to say "this isn't working out for me." [I really need to read my threads before I hit submit.]

    Anyway, I got it working now. I'm not familar with arrays and therefore did something wrong on my end. Re-read the code and wah-laa.

    Thanks again Freddie.
    <///////~

    www.sohh.com

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anytime
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •