Monk3h Posted June 24, 2008 Share Posted June 24, 2008 Im trying to put an if string inside a variable define string. Any ideas on the syntax? Example: $key = "Empty"; $keycheck = if ($key == 'Empty'){ // Line 7 Print "<img src=images/keyneautural.php>"; }; Error: Parse error: syntax error, unexpected T_IF in /home/monk3h/public_html/projectalpha/alphaground.php on line 7 Link to comment https://forums.phpfreaks.com/topic/111655-name-ifstring/ Share on other sites More sharing options...
DyslexicDog Posted June 24, 2008 Share Posted June 24, 2008 <?php $key = "Empty"; if ($key == 'Empty'){// Line 7 $keycheck = "<img src=images/keyneautural.php>"; print $keycheck; }; ?> Link to comment https://forums.phpfreaks.com/topic/111655-name-ifstring/#findComment-573141 Share on other sites More sharing options...
nashruddin Posted June 24, 2008 Share Posted June 24, 2008 More simpler syntax: $key = 'empty'; $keycheck = ($key == 'empty') ? '<img src="images/keyneautural.php">' : ''; Link to comment https://forums.phpfreaks.com/topic/111655-name-ifstring/#findComment-573143 Share on other sites More sharing options...
DyslexicDog Posted June 24, 2008 Share Posted June 24, 2008 Some folks have a hard time reading the if shorthand. Link to comment https://forums.phpfreaks.com/topic/111655-name-ifstring/#findComment-573319 Share on other sites More sharing options...
Monk3h Posted June 24, 2008 Author Share Posted June 24, 2008 I need the IF statment inside the $variable. so that when ever i use $keycheck the same if statement is repeated. Link to comment https://forums.phpfreaks.com/topic/111655-name-ifstring/#findComment-573475 Share on other sites More sharing options...
discomatt Posted June 24, 2008 Share Posted June 24, 2008 You want a function. Time to head back to PHP basics Link to comment https://forums.phpfreaks.com/topic/111655-name-ifstring/#findComment-573479 Share on other sites More sharing options...
Monk3h Posted June 24, 2008 Author Share Posted June 24, 2008 How do you do that? :S Link to comment https://forums.phpfreaks.com/topic/111655-name-ifstring/#findComment-573482 Share on other sites More sharing options...
KevinM1 Posted June 24, 2008 Share Posted June 24, 2008 How do you do that? :S <?php function keyCheck($key) { if($key == "empty") { $string = "<img src='images/keyneautural.php'>"; //are you sure you're not trying to spell 'neutral'? } else { $string = ""; } return $string; } $myImg = keyCheck("empty"); ?> Link to comment https://forums.phpfreaks.com/topic/111655-name-ifstring/#findComment-573490 Share on other sites More sharing options...
discomatt Posted June 24, 2008 Share Posted June 24, 2008 Alternately <?php function keyCheck($key) { if($key == "empty") return TRUE; return FALSE; } if ( keyCheck($someVar) ) echo '<img src="images/keyneautural.php">'; ?> Link to comment https://forums.phpfreaks.com/topic/111655-name-ifstring/#findComment-573495 Share on other sites More sharing options...
Monk3h Posted June 24, 2008 Author Share Posted June 24, 2008 Okay, im very Confused. What i'm trying to do is make a map. I have an 11X11 Table and in each of the 121 <td>s i have Redefined what $id is. Then after i Print the $keycheck. which should pick up on what the new ID is and then Print the image that is alocated to that id. Here is an example. $keycheck example: $check = mysql_fetch_array(mysql_query("SELECT * FROM `alphaground` WHERE `id` = '$id'")); if ($check[owner] == 0){ $keycheck = "<img src= images/keyneautural.png>"; }else{ $keycheck = "<img src= images/keyalliance.png>"; } Table Example: <td width='290' height='290' bgcolor='#66CCFF'><table width='210' border='0' align='center' cellpadding='0' cellspacing='0'> <tr> <td>"; $id = 1; Print "$keycheck </td> <td>"; $id = 2; Print "$keycheck </td> <td>"; $id = 3; Print "$keycheck </td> <td>"; $id = 4; Print "$keycheck </td> <td>"; $id = 5; Print "$keycheck </td> <td>"; $id = 6; Print "$keycheck </td> <td>"; $id = 7; Print "$keycheck </td> <td>"; $id = 8; Print "$keycheck </td> <td>"; $id = 9; Print "$keycheck </td> <td>"; $id = 10; Print "$keycheck </td> <td>"; $id = 11; Print "$keycheck </td> </tr> <tr> <td>"; $id = 12; Print "$keycheck </td> <td>"; $id = 13; Print "$keycheck </td> <td>"; $id = 14; Print "$keycheck </td> <td>"; $id = 15; Print "$keycheck </td> <td>"; $id = 16; Print "$keycheck </td> <td>"; $id = 17; Print "$keycheck </td> <td>"; $id = 18; Print "$keycheck </td> <td>"; $id = 19; Print "$keycheck </td> <td>"; $id = 20; Print "$keycheck </td> <td>"; $id = 21; Print "$keycheck </td> <td>"; $id = 22; Print "$keycheck </td> </tr> Link to comment https://forums.phpfreaks.com/topic/111655-name-ifstring/#findComment-573513 Share on other sites More sharing options...
Monk3h Posted June 24, 2008 Author Share Posted June 24, 2008 bump? Link to comment https://forums.phpfreaks.com/topic/111655-name-ifstring/#findComment-573585 Share on other sites More sharing options...
KevinM1 Posted June 24, 2008 Share Posted June 24, 2008 Sounds like you need to employ a while loop: <?php $query = "SELECT * FROM alphaground WHERE id = '$id'"; $result = mysql_query($query); $count = 1; $id = 1; echo "<table width='210' border='0' align='center' cellpadding='0' cellspacing='0'>"; while($check = mysql_fetch_assoc($result)) { if($check['owner'] == 0) { $keycheck = "<img src='images/keyneautural.png'>"; } else { $keycheck = "<img src='images/keyalliance.png'>"; } if($count > 11) //for every 12 items, create a new row { echo "</tr><tr><td>$id $keycheck</td>"; $count = 1; } else { echo "<td>$id $keycheck</td>"; } $count++; $id++; } ?> This code isn't tested by any means, but it should put you on the right track. Link to comment https://forums.phpfreaks.com/topic/111655-name-ifstring/#findComment-573617 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.