How to select displayed query results in php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robroy555
Forum Newbie
Posts: 3
Joined: Fri Nov 26, 2010 9:27 am

How to select displayed query results in php?

Post by robroy555 »

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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to select displayed query results in php?

Post by Celauran »

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.
robroy555
Forum Newbie
Posts: 3
Joined: Fri Nov 26, 2010 9:27 am

Re: How to select displayed query results in php?

Post by robroy555 »

<?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?
robroy555
Forum Newbie
Posts: 3
Joined: Fri Nov 26, 2010 9:27 am

Re: How to select displayed query results in php?

Post by robroy555 »

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>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to select displayed query results in php?

Post by Celauran »

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?

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>
Note that the form doesn't actually do anything yet.
Post Reply