twilitegxa Posted August 17, 2009 Share Posted August 17, 2009 I have the following code, but I need some help with my if statement. I was to display the records in the "attacks" table to display on a new row under the "$sub_attribute_names_attribute" variable if the value "Sailor Scout Attack" is displayed in the table (attacks table). //gather attacks $get_attacks = "select * from attacks where identity = '$identity'"; $get_attacks_res = mysql_query($get_attacks, $conn) or die(mysql_error()); while ($attacks_info = mysql_fetch_array($get_attacks_res)) { $attack_id = $attacks_info['id']; $attack_identity = $attacks_info['identity']; $attack = $attacks_info['attack']; $attack_level = $attacks_info['level']; $display_block .= " <tr> <td class=indent>$sub_attribute_names_attribute</td>"; if ($sub_attribute_names_attribute == 'Sailor Scout Attack') { echo <tr><td>$attack</td></tr>; } $display_block .= " <td>$sub_attributes_level_id</td> <td> </td> </tr>"; } I know my if statement is wrong because it's not displaying anything. Can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/170714-solved-if-statement-help/ Share on other sites More sharing options...
Maq Posted August 17, 2009 Share Posted August 17, 2009 Looks like this line: echo $attack; should be: $display_block .= "$attack"; Or, if you wanted to display it directly after the if is executed: echo "$attack"; If you turned error reporting on you would have seen this. ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/170714-solved-if-statement-help/#findComment-900390 Share on other sites More sharing options...
GingerRobot Posted August 17, 2009 Share Posted August 17, 2009 Well there's nothing syntactically wrong with your if statement, so i suggest you find out what is stored in $sub_attribute_name_attribute (incidentally, i'm all for descriptive variable names -- but sheesh). You can then go about finding out why it doesn't contain the value you thought it should. Echo out the variable prior to the if statement. Or, to put it another way, i'm sleepy and didn't notice the missing quotes. Quote Link to comment https://forums.phpfreaks.com/topic/170714-solved-if-statement-help/#findComment-900394 Share on other sites More sharing options...
twilitegxa Posted August 17, 2009 Author Share Posted August 17, 2009 Thanks, that got it! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/170714-solved-if-statement-help/#findComment-900406 Share on other sites More sharing options...
twilitegxa Posted August 17, 2009 Author Share Posted August 17, 2009 How can I make the $sub_attributes_name_attribute table row not repeat as the $attacks get listed under the "Sailor Scout Attack"? I have: $get_sub_attributes = "select * from scout_sub_attributes where identity = '$identity'"; $get_sub_attributes_res = mysql_query($get_sub_attributes, $conn) or die(mysql_error()); while ($sub_attributes_info = mysql_fetch_array($get_sub_attributes_res)) { $sub_attributes_id = $sub_attributes_info['id']; $sub_attributes_identity = $sub_attributes_info['identity']; $sub_attributes_attribute_id = $sub_attributes_info['sub_attribute_id']; $sub_attributes_level_id = $sub_attributes_info['level_id']; $get_sub_attribute_names = "select * from sub_attributes where id = '$sub_attributes_attribute_id'"; $get_sub_attribute_names_res = mysql_query($get_sub_attribute_names, $conn) or die(mysql_error()); while ($sub_attribute_names_info = mysql_fetch_array($get_sub_attribute_names_res)) { $sub_attribute_names_id = $sub_attribute_names_info['id']; $sub_attribute_names_attribute = $sub_attribute_names_info['sub_attribute']; //gather attacks $get_attacks = "select * from attacks where identity = '$identity'"; $get_attacks_res = mysql_query($get_attacks, $conn) or die(mysql_error()); while ($attacks_info = mysql_fetch_array($get_attacks_res)) { $attack_id = $attacks_info['id']; $attack_identity = $attacks_info['identity']; $attack = $attacks_info['attack']; $attack_level = $attacks_info['level']; $display_block .= " <tr> <td class=indent>$sub_attribute_names_attribute</td>"; if ($sub_attribute_names_attribute == 'Sailor Scout Attack') { $display_block .= " <tr> <td>$attack</td> <td>$attack_level</td> <td> </td> </tr>"; } $display_block .= " <td>$sub_attributes_level_id</td> <td> </td> </tr>"; } } } And the "$attacks" are being output as expected, but the $sub_attributes_names_attribute" row is repeating, which I do not want. Here is an image: See how it is repeating? This is how it should look: And each "attack" should be listed and then the rest of the "$sub_attribute_names_attribute" should be listed afterwards. Quote Link to comment https://forums.phpfreaks.com/topic/170714-solved-if-statement-help/#findComment-900415 Share on other sites More sharing options...
Maq Posted August 17, 2009 Share Posted August 17, 2009 Change this line: $get_attacks = "select * from attacks where identity = '$identity'"; to: $get_attacks = "select id, identity, level, DISTINCT attack from attacks where identity = '$identity'"; Quote Link to comment https://forums.phpfreaks.com/topic/170714-solved-if-statement-help/#findComment-900417 Share on other sites More sharing options...
twilitegxa Posted August 17, 2009 Author Share Posted August 17, 2009 I am receiving this error when I changed that line you suggested: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT attack from attacks where identity = 'Sailor Moon'' at line 1 What does this mean and how do I fix it? Quote Link to comment https://forums.phpfreaks.com/topic/170714-solved-if-statement-help/#findComment-900420 Share on other sites More sharing options...
Maq Posted August 17, 2009 Share Posted August 17, 2009 I don't really see anything wrong with it. Try putting DISTINCT first: $get_attacks = "select DISTINCT attack, id, identity, level from attacks where identity = '$identity'"; If that doesn't work then you can try a GROUP BY: $get_attacks = "select * from attacks where identity = '$identity' GROUP BY attack"; Other than that I'm not sure. Quote Link to comment https://forums.phpfreaks.com/topic/170714-solved-if-statement-help/#findComment-900428 Share on other sites More sharing options...
twilitegxa Posted August 17, 2009 Author Share Posted August 17, 2009 Well, for some reason, neither of those statements changed anything. The sub-attributes are still repeating. :-( Any other suggestions anyone? Quote Link to comment https://forums.phpfreaks.com/topic/170714-solved-if-statement-help/#findComment-900433 Share on other sites More sharing options...
twilitegxa Posted August 17, 2009 Author Share Posted August 17, 2009 Sorry if there was any confusion, there was another post I had with the same name as this one. This one is still unsolved! Quote Link to comment https://forums.phpfreaks.com/topic/170714-solved-if-statement-help/#findComment-900479 Share on other sites More sharing options...
twilitegxa Posted August 18, 2009 Author Share Posted August 18, 2009 I'm going to re-post the second part of this question since the first part has been resolved. Quote Link to comment https://forums.phpfreaks.com/topic/170714-solved-if-statement-help/#findComment-900580 Share on other sites More sharing options...
Maq Posted August 18, 2009 Share Posted August 18, 2009 I'm going to re-post the second part of this question since the first part has been resolved. Yes, you will probably get more responses that way, rather than people seeing 10+ posts and disregarding your thread. Quote Link to comment https://forums.phpfreaks.com/topic/170714-solved-if-statement-help/#findComment-900879 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.