Jump to content

Formatting output from a query


deslyxia

Recommended Posts

I have a query that I have run. I am able to output the dataset into a simple table it updates on my page beautifully. In the following code what I want to be able to do is along with the other columns i output have a 4th column that contains a button on each row and when a user clicks a button on the page it selects that particular row.

 

This is the code I have creating my table atm.

 

echo "<table alingn=\"center\" width=\"400px;\">";
		echo "<tr>
				  <td>ID</td>
				  <td>FirstName</td>
				  <td>LastName</td>
			  </tr>";
	   	while ($row = mysql_fetch_assoc($result)){
    		echo "<tr>
    		          <td>".$row['ID']."</td>
        		      <td>".$row['FName']."</td>
        		      <td>".$row['LName']."</td>
       		      </tr>";
   			}

   			echo "</table>";   

 

Basically everytime it loops through I would want the row that shows the ID to be a button that would execute a php script but pass it that ID. I dont really want the ID shown on the page.

 

 

Link to comment
https://forums.phpfreaks.com/topic/263908-formatting-output-from-a-query/
Share on other sites

Use a hidden input tag.

 

I gave this a shot but I get an error

 

while ($row = mysql_fetch_assoc($result)){
    		echo "<tr>
    		          <td>
				  	<form id=".$row['ID']." name=".$row['ID']." method="post" action="p_select.php">
				  		<input type="hidden" name="PID" value=".$row['ID'].">
						<input type="submit" value="Submit">
				  	</form>
				  </td>
        		      <td>".$row['FName']."</td>
        		      <td>".$row['LName']."</td>
       		      </tr>";
   			}

 

the error I get is Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'   

The php string you are trying to echo starts and ends with double quotes - echo " ....... "; You are also trying to use double-quotes inside of that string around html attribute values. The first double-quote after the initial one closes the quoted string and anything after that is outside of the string and produces php syntax errors.

 

The easiest fix would be to use single-quotes for everything that is inside of that echo " ....... "; statement. You can also put php array variables directly inside of a double-quoted string by surrounding them with {}.

 

The following is the simplest php/html syntax that will do what you want -

 

while ($row = mysql_fetch_assoc($result)){
echo "<tr>
		<td>
			<form method='post' action='p_select.php'>
				<input type='hidden' name='PID' value='{$row['ID']}'>
				<input type='submit' value='Submit'>
			</form>
		</td>
		<td>{$row['FName']}</td>
		<td>{$row['LName']}</td>
	</tr>";
}

 

BTW - there's no need to give html elements id and name attributes unless you are actually going to reference those attributes. I did not include the id and name you had in your <form> tag. You might actually want to add a name attribute to the submit button so that you can test in the p_select.php code if a form was actually submitted.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.