AnAmericanGunner Posted September 8, 2011 Share Posted September 8, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246674-array-help/ Share on other sites More sharing options...
gizmola Posted September 8, 2011 Share Posted September 8, 2011 This line: $array1 = array($name); doesn't make much sense in this context. The array() defines an array. I'm not sure what $name is suppossed to be here. Quote Link to comment https://forums.phpfreaks.com/topic/246674-array-help/#findComment-1266662 Share on other sites More sharing options...
AnAmericanGunner Posted September 8, 2011 Author Share Posted September 8, 2011 It's the variable I am pulling from my query. I want it to repeat $name for all the conditions that are met, as I explained. Quote Link to comment https://forums.phpfreaks.com/topic/246674-array-help/#findComment-1266664 Share on other sites More sharing options...
gizmola Posted September 8, 2011 Share Posted September 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246674-array-help/#findComment-1267113 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 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"'; Quote Link to comment https://forums.phpfreaks.com/topic/246674-array-help/#findComment-1267119 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.