FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Adding link to each value using foreach();





ncwdavid
Hey, I am trying to make a script where you can setup a sale like item name ,description amount and so on and it works from phpbb cash mod. So lets say you setup a sale and then it gets added to the database and on a certain page that shows the newest things up for sale. This is the temporary file that shows the newest items up for sale:
Code:

<table border="1">
<tr>
<th>Name</th><th>Description</th><th>Other</th><th>Amount</th>
</tr>
<?php
      include('../db.php');
$sql = "SELECT item_name, item_description, item_other, amount FROM sale";
$resource = mysql_query($sql) or die('Error in query: ' . mysql_error());
while ($row = mysql_fetch_row($resource)) {
    echo '<tr>';
    foreach ($row as $value) {
        echo "<td>$value</td>";
    }
    echo '</tr>';
}   
        ?>
        </table>


it works perfect and shows all the required stuff. This is where the problem occors that i cant figure out. In the database each new sale setup gets a sale_id e.g 1,2 and so on. In the page that shows the newest sales i want to be able to add a link to each item name like http://www.mydomain.com/sales/index.php?id=1 and the user can click on it to get to another page where i can use the $_GET[] to get the user id and retrieve the info for that sale _id.
If you have any idea could you please share them? i really need to get this figured out. Thanks in advance.
hexkid
Code:
<?php
      include('../db.php');

########
# you need to fetch the id from the database
/*
$sql = "SELECT item_name, item_description, item_other, amount FROM sale";
*/
$sql = "SELECT item_name, item_description, item_other, amount, item_id FROM sale";
$resource = mysql_query($sql) or die('Error in query: ' . mysql_error());
while ($row = mysql_fetch_row($resource)) {
    echo '<tr>';

########
# you need to identify which value is the item_id in order
# to use it in the link
/*
    foreach ($row as $value) {
        echo "<td>$value</td>";
    }
*/
echo "<td><a href=\"http://www.mydomain.com/sales/index.php?id=",
      "{$row['item_id']}\">{$row['item_name']}</a></td>";
echo "<td>{$row['item_description']}</td>";
echo "<td>{$row['item_other']}</td>";
echo "<td>{$row['amount']}</td>";
    echo '</tr>';
}   
        ?>
        </table>
ncwdavid wrote:
If you have any idea could you please share them?
ncwdavid
Could you explain it in a bit more detail. I do not understand where you put the comments what to do there? thanks
hexkid
ncwdavid wrote:
Could you explain it in a bit more detail. I do not understand where you put the comments what to do there? thanks


The ### comments are my thoughts about that particular area in the script; you can ignore/delete them.
The /* */ comments delimit your code. I left your code in for easy comparison with mine. You can ignore/delete them.
ncwdavid
I finally figured it out. this is what i did:
Code:

<?php
include('db.php');
$sql = "SELECT item_name, amount, sale_id FROM sale";
$resource = mysql_query($sql) or die('Error in query: ' . mysql_error());
while ($row = mysql_fetch_array($resource)) {
    echo '<tr>';
echo "<td><a href='http://www.tradexchange.frih.net/sales/view.php?id={$row['sale_id']}'>{$row['item_name']}</td>";
    echo '</tr>';
}   
        ?>
hexkid
ncwdavid wrote:
I finally figured it out.
YAY!
But if are not going to use the amount, you needn't select it from the database (you save a few nanoseconds).
Code:
$sql = "SELECT item_name, sale_id FROM sale";
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.