Jump to content

[SOLVED] if statement help


twilitegxa

Recommended Posts

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?

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:

 

error.jpg

 

See how it is repeating? This is how it should look:

 

correct.jpg

 

And each "attack" should be listed and then the rest of the "$sub_attribute_names_attribute" should be listed afterwards.

Link to comment
Share on other sites

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'";

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.