Jump to content

Create Array From Query Use Data Outside While Loop


BigTime

Recommended Posts

I have a form that needs to require certain parameters be set prior to showing it.

 

I query one column to find the conditions in which it might be required

 

the result set into an array

 

if a currently set parameter is found within this array then check for second parameter being set

 

if its set = show the form

if its not set = offer choices to set so we can show the form

 

My problem is in the damn while loop

 

How can I take the data that comes out of the initial $results query and store them for use OUTSIDE a while loop?

 

Thanks in adcance for any help

 

# setup SQL statement
$SQL = " SELECT DISTINCT column FROM table WHERE condition='$condition' AND condition2 != '' ";

# execute SQL statement
$result = mysql_db_query($db, $SQL, $cid);

# check for errors
if (!$result) { echo( mysql_error()); }

$check = mysql_num_rows($result);

 #We have a split needed so print it
 if($check>'1') {
   while ($data=mysql_fetch_array($result)){
   $columnresults=$data[column];
  $columnarray = array($columnresults);

  #If our current division selected is in this array then show AND REQUIRE the split choice to show the form
   $this = $_GET['this'];
   if (in_array($this, $columnarray))
   {
 if (!$that || $that=='')
 {
 echo (" THERE IS NO THAT WE NEED TO PROVIDE OPTIONS TO SET IT FIRST <BR>");
 }

 elseif ($that || $that != '')
 {
 echo ("THAT IS SET SHOW THE FORM<BR>");
 }
   }
   else (!in_array($this, $columnarray)){
 echo ("I DONT NEED THAT I CAN JUST SHOW THE FORM<BR>");
   }
  }
 }

Or define new variables from array results?

 

I dont need to loop my form 4 times I just want to know if what Im looking for is WITHIN the array in order to move to my next step

 

If it is in the array - Check something else is set, YES show form, NO show choices

If it is not in array - Show form

 

The while loop makes it want to loop as many times as my results are - so 4 results, 4 loops.

 

Im sure Im using the wrong method here.

 

Hoping someone smart out there understands what Im trying to do :)

You can always just store your results into an array and then use them later, eg:

$queryResults = array();
while ($data=mysql_fetch_assoc($results)){
  $queryResults[] = $data;
}
//Now $queryResults holds all the rows/columns returned.

 

If you just want the one column in an array by itself, then do that.

$columnValues = array();
while ($data=mysql_fetch_assoc($results)){
  $columnValues[] = $data['column'];
}
//Now $columnValues holds all the values from the specified column.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.