Hi all,
I have some code that grabs urls from a table in my database, and then displays them in a drop down menu as a basic search criteria.....However, the nature of the table dictates that a url will be entered numerous times (as its a table that stores transaction details about specific urls).
My problem lies in that the code I'm using will display the url in the drop down menu 3 times (if three entries are in the database table).
What I would ideally like would be for the url to be displayed only once in the drop down menu, but have the script return results for all urls that match the selected one when the form is posted. here is my code:
txnsearch.php
<?php require ("database.php"); ?>
<?php
$txns = @mysql_query('SELECT id, url FROM transaction');
if (!$txns) {
echo "<p>Unable to retrieve transaction details</p>";
}
?>
<form action="txn_search_results.php" method="post">
<p>Select a URL from the list</p>
<select name="tid" size="1">
<?php
while ($txn=mysql_fetch_array($txns)) {
$tid = $txn['id'];
$url = $txn['url'];
echo "<option value='$tid'>$url</option>";
}
?>
</select>
<input type="submit" name="Submit" value="Submit">
</form>
txn_search_results.php
<?php
if($logged_in){
$tid = $_POST['tid'];
?>
<table width="90%" border="0" class="column_right">
<tr>
<td class="faq">Date</td>
<td class="faq">Amount</td>
<td class="faq">Transaction ID</td>
<td class="faq">Senders Email</td>
</tr>
<tr>
<td colspan="5" class="faq"><hr size="1" /></td>
</tr>
<?php $txns=@mysql_query("SELECT DISTINCT id, url, date, amount, txnid, sendersemail FROM transaction WHERE id=$tid");
if (!$txns) {
echo "Unable to perform search";
}
while ($txn=mysql_fetch_array($txns)) {
echo "<tr>";
$tid = $txn['id'];
$date = $txn['date'];
$amount = $txn['amount'];
$txnid = $txn['txnid'];
$sendersemail = $txn['sendersemail'];
echo "<td class=\"box_text\">$date</td>
<td class=\"box_text\">$amount</td>
<td class=\"box_text\">$txnid</td>
<td class=\"box_text\">$sendersemail</td>
</tr>";
}
?>
</table>
<p><a href="index.php">New Search</a></p>
<?php
}
else{
displayLogin();
}
?>
Any help greatfully appreciated. This is a script that I've written from the ground upwards, as I'm getting much more involved with php now, so also any pointers about 'best practices' etc would great! 🙂
Many thanks in advance
Mark