Hi,
I'm working on a computer sales website for a college project. I know about how to connect to mysql and return results and put into talbe for display after executing query.
My scenario is this:
I select lets say 10 rows from mysql and display. I need the option for user to select one of these ten rows, by click or radio button or checkbox. Then i want to process that particular row which was selected among 10 rows. Can anyone please help me about what i need for this purpose? thank you.
How to select displayed query results in php?
Moderator: General Moderators
Re: How to select displayed query results in php?
Display the results in a form. Add your radio button or checkbox to each row and assign it a value of the item's id.
Re: How to select displayed query results in php?
<?php
include "config.php";
include "opendb.php";
$query = "SELECT first_name, last_name from firstlastname";
$result = mysql_query($query);
include "closedb.php";
?>
<html>
<head>
</head>
<body>
<table border = "1">
<tr>
<?php
while ($field = mysql_fetch_field($result))
{
print"<th> $field->name </th>";
}
?>
how can i display first and last name here with the third field radio button added
with value = "firstNameValue";
</table>
</body>
</html>
what i intend to do here, is after getting list of f_name and l_name from table, want to add radio button to each field.
So at the end, i get a radio button field value associated with f_name. I could use this type of query later to process my item data for project.
thanks a lot in advance.
Also, is there a better way to write the above html form to display results?
include "config.php";
include "opendb.php";
$query = "SELECT first_name, last_name from firstlastname";
$result = mysql_query($query);
include "closedb.php";
?>
<html>
<head>
</head>
<body>
<table border = "1">
<tr>
<?php
while ($field = mysql_fetch_field($result))
{
print"<th> $field->name </th>";
}
?>
how can i display first and last name here with the third field radio button added
with value = "firstNameValue";
</table>
</body>
</html>
what i intend to do here, is after getting list of f_name and l_name from table, want to add radio button to each field.
So at the end, i get a radio button field value associated with f_name. I could use this type of query later to process my item data for project.
thanks a lot in advance.
Also, is there a better way to write the above html form to display results?
Re: How to select displayed query results in php?
please see this code...
<?php
include "config.php";
include "opendb.php";
$query = "SELECT first_name, last_name from firstlastname";
$result = mysql_query($query);
include "closedb.php";
?>
<html>
<head>
</head>
<body>
<table border = "1">
<?php
while ($row = mysql_fetch_assoc($result))
{
print "<tr>\n";
foreach ($row as $col => $val) {
print "<td> $val </td>";
}
HAVE TO PUT RADIO BUTTON HERE.. dont know how to print in html....
print("<input type = "radio" value = "$val">");
print "</tr>";
}
?>
</table>
</body>
</html>
<?php
include "config.php";
include "opendb.php";
$query = "SELECT first_name, last_name from firstlastname";
$result = mysql_query($query);
include "closedb.php";
?>
<html>
<head>
</head>
<body>
<table border = "1">
<?php
while ($row = mysql_fetch_assoc($result))
{
print "<tr>\n";
foreach ($row as $col => $val) {
print "<td> $val </td>";
}
HAVE TO PUT RADIO BUTTON HERE.. dont know how to print in html....
print("<input type = "radio" value = "$val">");
print "</tr>";
}
?>
</table>
</body>
</html>
Re: How to select displayed query results in php?
First, I'd add ID to your SELECT statement (assuming that field exists -- what happens if you get two people named Bob Smith?).
Maybe something like this?
Note that the form doesn't actually do anything yet.
Maybe something like this?
Code: Select all
<?php
$sql = "SELECT id, first_name, last_name FROM firstlastname";
$res = mysql_query($sql);
?>
<form id="frmName" action="" method="post">
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>{$row['first_name']}</td>";
echo "<td>{$row['last_name']}</td>";
echo "<td><input type=\"checkbox\" name=\"id\" value=\"{$row['id']}\" /></td>";
echo "</tr>";
}
?>
</tr>
<tr>
<td colspan="3"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>