Solved

adding items from database into an array

Posted on 2007-11-20
5
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

688 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