markvaughn2006 Posted September 19, 2009 Share Posted September 19, 2009 I'm making a game, and when you go to a location, you see a list of who else is there by this code... <?php $result = mysql_query("SELECT user_name FROM users_tbl WHERE location='Ye Olde Tavern'") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo $row['user_name']; echo '<br />'; }?> So the result is something like : Bob Tom Sally Ed I want to display the names of who is there and also add next to it a "submit" button but with the caption "Attack" or something similar and when the Attack button is pressed next to Bob, it runs an attack.php that will automatically use "Bob" as the variable to be "attacked". So ideally the output would look something like... Bob (attack) Tom (attack) Sally (attack) Ed (attack) with the (attack) being a submit button. Any pointers as to how to make this happen? I greatly appreciate all of you guy's help! Quote Link to comment https://forums.phpfreaks.com/topic/174814-solved-echo-attackrowuser_name/ Share on other sites More sharing options...
ozestretch Posted September 19, 2009 Share Posted September 19, 2009 <?php $result = mysql_query("SELECT user_name FROM users_tbl WHERE location='Ye Olde Tavern'") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo '<form method="post" action="attack.php">.'$row['user_name'].'<input type="hidden" name="attackee" value="'.$row['user_name'].'" /><input type="submit" value="Attack" /></form>'; echo '<br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/174814-solved-echo-attackrowuser_name/#findComment-921275 Share on other sites More sharing options...
ozestretch Posted September 19, 2009 Share Posted September 19, 2009 updated above Quote Link to comment https://forums.phpfreaks.com/topic/174814-solved-echo-attackrowuser_name/#findComment-921278 Share on other sites More sharing options...
Psycho Posted September 19, 2009 Share Posted September 19, 2009 Pointer #1 use CODE tags when posting !!! I'm assuming that each user has an ID (and that the current user's ID is stored in a session variable). You will want to pass the ID of the user to be attacked when the "Attack" is clicked. You can either use a hyperlink where the link has the user to be attacked ID is included or you can use a button with a form (you could use a button with just JavaScript, but that is typically frowned upon). If using a button and a form you can use either GET or POST. Some people do not like using GET because they don't want the user to see the data on the query string. But, using a form with GET allows you to use a form or normal hyperlink or both. So, I will use that for the example. I would also suggest separating your logic from the actual HTML output. Makes things much easier. <?php $query = "SELECT id, user_name FROM users_tbl WHERE location='Ye Olde Tavern'"; $result = mysql_query($query) or die(mysql_error()); $attackList = ''; while ($row = mysql_fetch_array($result)) { $attackList .= "{$row['user_name']}<br />"; $attackList .= "<form action="attack.php" method="GET">"; $attackList .= "<input type="hidden" name="attack_id" value="{$row['id']}">"; $attackList .= "<button type="submit">Attack</button>"; $attackList .= "</form><br /> "; } ?> Rivals:<br /> <?php echo $attackList; ?> NOTE: FORMs have default padding top and bottom which might spread our the names vertically more than you would like. You can "fix" that by including style attributes in your form tags or by using a class or style properties on the page as a whole. Quote Link to comment https://forums.phpfreaks.com/topic/174814-solved-echo-attackrowuser_name/#findComment-921287 Share on other sites More sharing options...
markvaughn2006 Posted September 19, 2009 Author Share Posted September 19, 2009 Hey Ozestretch, thanks for the fast reply!! It's giving me an error though... Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/content/55/4878855/html/game/who/burgerqueentest.php on line 34 any idea? Thanks again! mjdamato- Each user has an ID and it is stored in a session variable, I'll check out your suggestion too, thanks for the quick replies! very helpful Quote Link to comment https://forums.phpfreaks.com/topic/174814-solved-echo-attackrowuser_name/#findComment-921291 Share on other sites More sharing options...
ozestretch Posted September 19, 2009 Share Posted September 19, 2009 php">.'$row['user should be php">'.$row['user decimal in wrong spot Quote Link to comment https://forums.phpfreaks.com/topic/174814-solved-echo-attackrowuser_name/#findComment-921294 Share on other sites More sharing options...
markvaughn2006 Posted September 19, 2009 Author Share Posted September 19, 2009 boo-ya, thanks Quote Link to comment https://forums.phpfreaks.com/topic/174814-solved-echo-attackrowuser_name/#findComment-921337 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.