Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/174814-solved-echo-attackrowuser_name/
Share on other sites

<?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 />';
   }
?>

 

 

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.

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

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.