Solved

How to break an array into individual variables

Posted on 2006-10-22
2
199 Views
Last Modified: 2012-06-21
Here is the deal

I have a form where the user can select multiple items using checkboxes after the form is submited the script query the database (MySQL) and pull the data according to the checkboxes selected.

Here is the script to pull the data.

<?

                $section_id=array(); $section_id=$_POST['Record_id'];            

                     foreach($section_id as $value) {
                     
$query1 = mysql_query("SELECT street
                                    FROM City
                                    WHERE section_id = '$value'");
                         
 while($row2 = mysql_fetch_array($query1))
 
 echo "<p>".$row2['street']."";
  }
 
?>

The script works great and will echo all the streets on a different line as expected.

But what I need is to be able to break the array in individual parts and assign those parts to variables, so instead of getting something like

Majorstreet
Minorstreet
Sidestreet

I would get

$var1 = "Majorstreet";
$var2 = "Minorstreet";
$var3 = "Sidestreet";

Then I would be able to use the variables where I want in the display page.

Hope it make sense, should not be to hard for an Expert, an easy 500 pts. mostly because I'd like to get an answer asap.

Gamebits
0
Comment
Question by:gamebits
2 Comments
 
LVL 29

Accepted Solution

by:
TeRReF earned 500 total points
Comment Utility
This should give you $var1, $var2, etc...
<?

                $section_id=array(); $section_id=$_POST['Record_id'];            

                     foreach($section_id as $value) {
                     
$query1 = mysql_query("SELECT street
                                    FROM City
                                    WHERE section_id = '$value'");
 $i = 1;                      
 while($row2 = mysql_fetch_array($query1))
 
 echo "<p>".$row2['street']."";
 $var = 'var'.$i;
 $$var = $row2['street'];
 $i++;
  }
 
?>
0
 
LVL 28

Author Comment

by:gamebits
Comment Utility
TeRReF

Your solution did not work as is but you put me on the right track, thanks.

Here is the solution that worked for me.

<?
                $section_id=array(); $section_id=$_POST['Record_id'];            

                     foreach($section_id as $value) {
                     
$query1 = mysql_query("SELECT street
                                    FROM City
                                    WHERE section_id = '$value'");
                     
 while($row2 = mysql_fetch_array($query1))
 
$var[] = "".$row['street']."";

 $street1 = "$var[0]";
 $street2 = "$var[1]";

?>

Gamebits
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now