I need to loop through a list that is generated by a <select> tag, so I can
query a lookup table. I managed to come up with this, 

<?php
        session_start();
        $_SESSION['sv_CampusList'] = "1,2,4,5"; // set elsewhere. Here for
demo/testing purposes
        $lst= split(",",$_SESSION['sv_CampusList']);
        foreach ($lst as $id){
                print("$id<br>");
        }
?>

And have modified it to query the lookup table, then the main table. Seems
to work ok.

Thanks,
J

-----Original Message-----
From: CPT John W. Holmes [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, August 27, 2003 7:23 AM
To: James Johnson; [EMAIL PROTECTED]
Subject: Re: [PHP] Looping through a list - Newbie question


From: "James Johnson" <[EMAIL PROTECTED]>
> I have a list contained in a session var. I want to loop through the 
> list, getting the value of the current item, then do a query based on 
> that
value.
> Is there an easy way to do this, or do I need to convert the list to 
> an array first? In the code below, $id isn't being set.
>
> Here's my code (this is just testing stuff, sv_CampusList is set 
> somewhere
> else):
> <?php
> session_start();
> $_SESSION['sv_CampusList'] = "1,2,4,5";
> for($i=1; $i<=strlen($_SESSION['sv_CampusList']); $i++){
> $id = strpos($_SESSION['sv_CampusList'],$i);
> echo $id;
> }
> ?>

What are you actually trying to accomplish here? I could tell you how to
loop through a list in a string and run a query for each number, but I'd
have the horrible feeling that you're using some inefficient method to
accomplish something.

For instance, if you're just trying to select data matching the numbers in
your list, you can do this:

$query = "SELECT * FROM Table WHERE campus_list IN
({$_SESSION['sv_CampusList']})";

---John Holmes...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to