Jump to content

Array Help


AnAmericanGunner

Recommended Posts

Everywhere I searched for information on arrays hasn't been overly helpful, so I am wondering if you guys could help me.

 

I am attempting to have one query that does a very basic search in the database where Column A is yes, then I have it call up the rows for the matching information (Value 1, Value 2, Value 3).

 

Then, I want it to be if Value 2 = A Condition and Value = B Condition, echo ALL the value 1s that meet those conditions. I was pointed towards arrays by my friend but unfortunately she was leaving as I started this task.

 

I have my query and then the code to gather the values.

Then I have an if statement:

 

if($age == 0 && $gender == "Stallion") { $array1 = array($name); }

 

And then I want to echo all the values that array1 comes up with, but I'm not sure if I'm heading in the right direction or not. Any help is greatly appreciated :)

Link to comment
Share on other sites

That's about as clear as mud.

 

Where is your result set variable?  Are you doing this inside a fetch loop? 

 

Is your goal to filter results by those columns?  I'm just guessing because your explanation of what you want is really bad, which is why you have gotten no responses other than mine. 

 

I'm going to just take a swag here, because I'm practically clairvoyant after 10 years of answering questions here, and guess this is close to what you're after:

 

 


// You did a mysql_query like SELECT * FROM sometable WHERE somecol = 'something'
// $result = mysql_query....


while ($row = mysql_fetch_assoc($result) {
    $rows[] = $row; 
}

// Filter into 2nd array

$stallions = array();
foreach ($rows as $row) {
  if ($row['age'] == 0 && $row['gender'] == 'Stallion')
      $stallions[] = $row;
}

// Now you have filtered rows in $stallion
var_dump($stallions);

 

In the future, when someone here, especially with a badged account tells you that your question is not clear, I'd advise you strongly to go back through your question and try and clarify it. 

Link to comment
Share on other sites

You want to use

 

$array1[] = $name

 

What that will do is add a new key on the END of $array1 containing whatever value is in $name.

 

Here's a full example, with a print_r at the end to see what the array contains.

<?php 

// UNTESTED CODE
$q = 'SELECT `val1`, `val2`, `val3` FROM `table` WHERE `col` = "yes"';
$r = mysql_query( $q );

$data = array();

while( $row = mysql_fetch_assoc($r) ) {
if( $row['val2'] == 'foo' && $row['val3'] == 'bar' ) {
	$data[] = $row['val1'];
}
}

echo '<pre>';
print_r( $data );
echo '</pre>';

?>

 

Personally, I don't see why you don't just use something like

 

$q = 'SELECT `val1` FROM `table` WHERE `col` = "yes" AND `val2` = "foo" AND `val3` = "bar"';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.