Solved

adding items from database into an array

Posted on 2007-11-20
5
214 Views
Last Modified: 2008-02-01
hi, i am using this code to select from a database,


                              $query = "Select * from countys;";
                              $result1 = mysql_query($query);
                        
                              if (mysql_num_rows($result1) > 0)            
                              {
                                    while ($myrow1 = mysql_fetch_array($result1))
                                    {
                                    }
      
                              }


i want to put each item into an array, like this,

$option = array('--','County1','County2') etc etc...

how do i build the array option in the database code above?


0
Comment
Question by:markmchugh
  • 2
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 20320756
$query = "Select county from countys;";
                              $result1 = mysql_query($query);
                        $option=array();
                              if (mysql_num_rows($result1) > 0)            
                              {
                                    while ($myrow1 = mysql_fetch_array($result1))
                                    {
$option[]=$myrow1[0];
                                    }
     
                              }
0
 
LVL 82

Expert Comment

by:hielo
ID: 20320799
IF your query is returning more than one field, as in:
$query = "Select city, county from countys;";
                              $result1 = mysql_query($query);
                        $option=array();
                              if (mysql_num_rows($result1) > 0)            
                              {
                                    while ($myrow1 = mysql_fetch_assoc($result1))
                                    {
$temp=array("city" => $myrow1["city"], "county" =>$myrow1["county"] );
$option[]=$temp;
                                    }
     
You would then retrieve the values as follows:
for($i=0;$i<count($option);++$i)
{
echo( $option[$i]["city"] . " " . $options[$i]["county"]);
}
                              }

0
 
LVL 4

Expert Comment

by:simonkin
ID: 20321506
Hi,

Try this...


<?php
 

	// Perform the query

	$query = "Select * from countys;";

	$result = mysql_query($query);

                        

	// If there are results

	if (mysql_num_rows($result) > 0) {

	

		// Define the options array

		$options = array();

	

		// Loop through the resultset

		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

		

			// Write the options array

			$options[] = $row["county"]; // Assuming county is the name of the field you require...

		

		}

		

		// Check the output...

		print_r($options);

	

	}
 

?>

Open in new window

0
 

Author Comment

by:markmchugh
ID: 20321726
i want to put '--' a first position in the array, i'm using this for a select box,

this code does not do it

$option[] = '--';
                                    while ($myrow1 = mysql_fetch_array($result1))
                                    {
                                    $option[]=$myrow1[1];
                                    }
      
0
 
LVL 4

Accepted Solution

by:
simonkin earned 500 total points
ID: 20321775
Ok, try this...


<?php

 

        // Perform the query

        $query = "Select * from countys;";

        $result = mysql_query($query);

                        

        // If there are results

        if (mysql_num_rows($result) > 0) {
 

                // Define the options array

                $options = array();
 

                // Add the -- to the start of the array

                $options[] = "--";

        

                // Loop through the resultset

                while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

                

                        // Write the options array

                        $options[] = $row["county"]; // Assuming county is the name of the field you require...

                

                }

                

                // Check the output...

                print_r($options);

        

        }

 

?>

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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

20 Experts available now in Live!

Get 1:1 Help Now