Solved

How can I put MySQL fields into an array

Posted on 2009-04-15
9
258 Views
Last Modified: 2013-12-12
I'm trying to create and array that will be passed into a drop down ComboBox in Flash. It works fine If I put the text into the array but below where "field 1", "field 2", "field 3", "field 4", "field 5", "field 6" is needs to come from fields in MySQL. I can't seem to figure this out do I create a while or for loop to go inside the $NameArr or what?

Please help.
@mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");

@mysql_select_db("$db_name") or die ("no database");
 

$result = mysql_query("SELECT * FROM job_listings WHERE employer_id='$id'");
 

$NameArr = array("field 1", "field 2", "field 3", "field 4", "field 5", "field 6");
 
 

// below is the code that is working

$NameArr = array("field 1", "field 2", "field 3", "field 4", "field 5", "field 6");
 

$numReturn = count($NameArr);

$i = 0;

print "&";
 

	while ($i < $numReturn) {

		$Name 		= $NameArr[$i];
 

		print "Name$i=$Name&";

		$i++;

	}

print "&NumItems=$numReturn&Go=Yes&";

Open in new window

0
Comment
Question by:lallen30
  • 5
  • 3
9 Comments
 
LVL 14

Accepted Solution

by:
shobinsun earned 375 total points
ID: 24150120
0
 
LVL 1

Author Comment

by:lallen30
ID: 24151094
Thanks for such a quick reply I've added some code based on the code from that link but I have a problem. When I test the php page it just outputs this:

 &Name0=Array&Name1=Array&Name2=Array&Name3=Array&Name4=Array&Name5=Array&Name6=Array&Name7=Array&Name8=Array&&NumItems=9&Go=Yes&

I need it to output this:
&Name0=field 1&Name1=field 2&Name2=field 3&Name3=field 4&Name4=field 5&Name5=field 6&&NumItems=6&Go=Yes&


Any ideas on how I can fix this?
<?php

include_once "connect_to_mysql.php";

include_once "includes/id.php";
 

$result = mysql_query("SELECT * FROM job_listings WHERE employer_id='$id'");
 
 

// define the $feedList array

$feedList = array();
 

      while($row1 = mysql_fetch_array($result)){

     

            $feedid = $row1['employer_id'];

           

            $getfeedinfo = "SELECT * FROM job_listings WHERE employer_id='$id'";

            $feedinfo = mysql_query($getfeedinfo) or die(mysql_error());

           

                  while($feedinfocontent = mysql_fetch_array($feedinfo)){
 

                        $feedList[] = array(

                          'job_title' => $feedinfocontent['job_title'],

						  

                         );

                  }

      }

	  

	$numReturn = count($feedList);

	$i = 0;

	print "&";

	

		while ($i < $numReturn) {

			$Name 		= $feedList[$i];

	

			print "Name$i=$Name&";

			$i++;

		}

		

print "&NumItems=$numReturn&Go=Yes&";

?>

Open in new window

0
 
LVL 1

Assisted Solution

by:alexandremattar
alexandremattar earned 125 total points
ID: 24161728
Try:
while($feedinfocontent = mysql_fetch_array($feedinfo)){

 

                        $feedList[] = $feedinfocontent['job_title'];

                  }

Open in new window

0
 
LVL 1

Author Comment

by:lallen30
ID: 24164566
Thanks for your reply alexandremattar.
Unfortunately I'm getting this error now that I changed it:

Parse error: parse error in C:\wamp\www\collegejobs\read_job_post.php on line 25


Any other ideas would be much appreciated.
<?php

include_once "connect_to_mysql.php";

include_once "includes/id.php";
 

$result = mysql_query("SELECT * FROM job_listings WHERE employer_id='$id'");
 
 
 
 

// define the $feedList array

$feedList = array();
 

      while($row1 = mysql_fetch_array($result)){

     

            $feedid = $row1['employer_id'];

           

            $getfeedinfo = "SELECT * FROM job_listings WHERE employer_id='$id'";

            $feedinfo = mysql_query($getfeedinfo) or die(mysql_error());

           

                while($feedinfocontent = mysql_fetch_array($feedinfo)){

 

                        $feedList[] = $feedinfocontent['job_title'];

                  }

            }

      }

	  

	$numReturn = count($feedList);

	$i = 0;

	print "&";

	

		while ($i < $numReturn) {

			$Name 		= $feedList[$i];

	

			print "Name$i=$Name&";

			$i++;

		}

		

print "&NumItems=$numReturn&Go=Yes&";

?>

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 14

Assisted Solution

by:shobinsun
shobinsun earned 375 total points
ID: 24164830
Hello,

You have an extra '}' in your code. Please remove it. Use the original code as:

Regards
<?php

include_once "connect_to_mysql.php";

include_once "includes/id.php";

 

$result = mysql_query("SELECT * FROM job_listings WHERE employer_id='$id'");

 

 

 

 

// define the $feedList array

$feedList = array();

 

      while($row1 = mysql_fetch_array($result)){

     

            $feedid = $row1['employer_id'];

           

            $getfeedinfo = "SELECT * FROM job_listings WHERE employer_id='$id'";

            $feedinfo = mysql_query($getfeedinfo) or die(mysql_error());

           

                while($feedinfocontent = mysql_fetch_array($feedinfo)){

 

                        $feedList[] = $feedinfocontent['job_title'];

                  }

            }

      

          

        $numReturn = count($feedList);

        $i = 0;

        print "&";

        

                while ($i < $numReturn) {

                        $Name           = $feedList[$i];

        

                        print "Name$i=$Name&";

                        $i++;

                }

                

print "&NumItems=$numReturn&Go=Yes&";

?>

Open in new window

0
 
LVL 1

Author Comment

by:lallen30
ID: 24165036
You have been very helpful and thanks so much for catching that.
It's outputting the right info now but its repeating it serveral times.

Heres what it's outputing:
&Name0=Floor Supervisor&Name1=Janitor&Name2=Plumber&Name3=Floor Supervisor&Name4=Janitor&Name5=Plumber&Name6=Floor Supervisor&Name7=Janitor&Name8=Plumber&&NumItems=9&Go=Yes&

Even though there is only 3 rows it's repeating them and counting up to 9.
It's so close to being there. Is there anyway to fix this?

0
 
LVL 14

Assisted Solution

by:shobinsun
shobinsun earned 375 total points
ID: 24165343
Hello,

Use this    "$numReturn = mysql_num_rows($feedinfo);"

  instead of  

  "$numReturn = count($feedList);"

Regards
0
 
LVL 1

Author Comment

by:lallen30
ID: 24166820
Again Thanks so much. It works great.

For anyone else trying to do this below is the final code. First is the PHP and below that is the Actionscript 2.0.

// php file is ComboBox_PHP_Link.php - futher down is the actionscript

<?php

include_once "connect_to_mysql.php";

include_once "includes/id.php";
 

$result = mysql_query("SELECT * FROM job_listings WHERE employer_id='$id'");
 

// define the $feedList array

$feedList = array();
 

      while($row1 = mysql_fetch_array($result)){

     

            $feedid = $row1['employer_id'];

           

            $getfeedinfo = "SELECT * FROM job_listings WHERE employer_id='$id'";

            $feedinfo = mysql_query($getfeedinfo) or die(mysql_error());

           

                while($feedinfocontent = mysql_fetch_array($feedinfo)){

 

                        $feedList[] = $feedinfocontent['job_title'];

                  }

            }

      

	  

	$numReturn = mysql_num_rows($feedinfo);

	$i = 0;

	print "&";

	

		while ($i < $numReturn) {

			$Name 		= $feedList[$i];

	

			print "Name$i=$Name&";

			$i++;

		}

		

print "&NumItems=$numReturn&Go=Yes&";

?>
 
 
 
 

//--------------------------------- actionscript is:
 

// Main Functions.. This controls your Drop Down / Combo Box.
 

// The AddItems function assigns values to the drop down box from the external source

function AddItems() {

	for (i=0; i<NumItems; i++) {

		var Name 		= eval("Name"+i);

		var DataRow 	= eval("DataRow"+i);

   		dropDown.addItem(Name, DataRow); 

	}

	//Set ChangeHandler (this is the most important line).

	dropDown.setChangeHandler("SelectItem");

}
 

// The SelectItem function tells the movie what to do when a user

// Clicks on a certain Item in the drop down movie - this is your OnChange Handler.
 

// In this example we use it to open up a new URL.

function SelectItem(){

   URLName	= dropDown.getSelectedItem().label; 

   URL		= dropDown.getSelectedItem().data;

   

   getURL(URL, "_blank");

}
 
 

// Makes sure the initial value of Go is equal to zero

_root.Go = "";
 

// Tells the thinking MC to gotoAndPlay(2) - and wait.

_root.Thinking.gotoAndPlay(2);
 

// Specify the URL to either the Text file or PHP script that loads data from DB.

// Make sure to change the path to your Data Source.

loadVariables("http://localhost/collegejobs/test/phparray/ComboBox_PHP_Link/ComboBox_PHP_Link.php", this);

Open in new window

0
 
LVL 1

Author Closing Comment

by:lallen30
ID: 31570555
Thanks so much for your help you have been a life saver.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

932 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

11 Experts available now in Live!

Get 1:1 Help Now