PDA

View Full Version : Dynamic link in While loop?



Johan Beijar
03-05-2008, 09:35 AM
Hi,

I need help from you experts in resolving a problem...

I would like to display a table with a number of coloums and the column to the very right will have a link to a update-page for each product. I have two problems...

1. The Upd-link is not presenting the correct link. It shows the product id but also all the rest HTML-code for that specific row.

2. I get the footer into the very last cell of the Upd row...strange...

I would appriciate any help in this matter.

Thank you all,

//Johan Beijar


--Code--

<div id="content">

<?
echo "<h2>Products</h2>";
// Retrieve info
$result = mysql_query("SELECT brandid, productname, price, currency, recpriceretail, productnumber, productid FROM products WHERE username='$session->username'") or die(mysql_error());

// Format of table
echo "<table border='1' bordercolor='#cccccc' cellspacing='0' cellpadding='3'>";
echo "<tr bgcolor='#cccccc'><td>Brand</td>
<td>Prod name</td><td>Price</td> <td>Currency</td><td>Retail Price</td> <td>Prod number</td><td>Update</td></tr>";

// get the rows from the query
while($row = mysql_fetch_array( $result ))
{
// print the rows
echo "<tr><td>";
echo $row['brandid'];
echo "</td><td>";
echo $row['productname'];
echo "</td><td>";
echo $row['price'];
echo "</td><td>";
echo $row['currency'];
echo "</td><td>";
echo $row['recpriceretail'];
echo "</td><td>";
echo $row['productnumber'];
echo "</td><td>";
echo "<a href=\"userinfo.php?productid=" .$row['productid']. ">Upd</a>";
echo "</td></tr>";

// echo "<a href=\"userinfo.php?user=$uname\">$uname</a> / ";
//echo "<input type=\"checkbox\" name=\"size[]\" value=" .$roweusize[value_id]. ">";
}
echo "</table>";
?>
</div>
</div>
</div>
<div class="clearingdiv">&nbsp;</div>
</div>
</div>
<div id="footer">
<?
include("footer.php");
?></div>
</body>
</html>

--End Code--

city_coder
03-05-2008, 11:51 AM
Im hoping i can help with 1 of them. I saw your problem that it was putting everything in the link. Cant quite figure that 1 out, unless someone else can. Generally when i tend to do this, i just concat a string with the results in it.


<html>
<body>
<div id="content">
<?
error_reporting(E_ALL);
require_once ("dbconnect.php");
mysql_select_db($database_inqs, $inqs);
?>
<?
$name = 'mark';
echo "<h2>Products</h2>";
// Retrieve info
$result = mysql_query("SELECT * FROM products WHERE username = '$name'") or die(mysql_error());

// Format of table
echo "<table border='1' bordercolor='#cccccc' cellspacing='0' cellpadding='3'>";
echo "<tr bgcolor='#cccccc'><td>Brand</td>
<td>Prod name</td><td>Price</td> <td>Currency</td><td>Retail Price</td> <td>Prod number</td><td>Update</td></tr>";
// get the rows from the query
while($row = mysql_fetch_array( $result ))
{
//print $results at end
$results = "<tr>";
$results .= '<td>'.$row['brandid'].'</td>';
$results .= '<td>'.$row['productname'].'</td>';
$results .= '<td>'.$row['price'].'</td>';
$results .= '<td>'.$row['currency'].'</td>';
$results .= '<td>'.$row['recpriceretail'].'</td>';
$results .= '<td>'.$row['productnumber'].'</td>';
$results .= '<td><a href="userinfo.php?productid='.$row['productid'].'">Upd</a></td>';
$results .= "</tr>";

}
echo $results;
echo "</table>";
?>
</div>
<div class="clearingdiv">&nbsp;</div>
</div>
</body>
</html>


Starting from the top;
1. Put error_reporting(E_ALL); into your code, it gives you something back sometimes instead of just a nice informative blank page :P
2. I used a variable $name to query my database, i dont know how it would be set up in yours but im guessing something quite similar(or im hoping so).
3. Instead of asking to bring back specific columns from your database, bring back all of them(unless u have loads, but then ur DB would be wrong). //SELECT *
4.You can probably see what iv done, just simply added on to/extended/concatenated(choose which ever) :P makes things easier to read i think(personal opinion)
5. Echo out that variable when youve finished with it.

I cant help you with the footer problem, but its not appearing in the link anymore(well not for me)

Hope this helps :D

Johan Beijar
03-05-2008, 06:07 PM
Hi,

A 1000 thanks for this, I truely appriciate it and it solved my two problems.

Although 2 small things...
1. You forgott a "." on the row: $results = "<tr>";
it should be:
$results. = "<tr>";

(I point it out just so others can reuse your excellent code as well)

2. Thanks for the hint with "error_reporting(E_ALL); ". Good function.
I get the following message: "Notice: Undefined variable: results in ....."
What does this mean? Should I do anything about it.

Thank you,

//Johan Beijar

city_coder
03-05-2008, 07:06 PM
You dont need to do the .= the first time your going to concatentate a variable.
The First time you use it, its just like declaring and setting a variable and anything after that you use .=

Have you changed anything in particular or of substance from what i put up? I didnt seem to find anything wrong with it, if you have changed it then i might be able to help.

From what i know, error_reporting(E_ALL); will bring back notices as well as full blown errors. Sort of like you see in IE 'Page done but with errors'.

Side note, that error_reporting(E_ALL); should only be used when its you(the developer) seeing the code. Its not necessary or not so much good practice when it is live. For when its live you need an error checker/parser which will give a much more user friendly msg back to the user.

Johan Beijar
03-06-2008, 07:57 PM
Ok, thank you for the additional info.

I use the following code and if I remove the first .= the result will only show one article instead of many...

"Only" thing that I have changed is the query and I am not selecting *, that did not work either.

Once again, thank you for your efforts!

//Johan Beijar



// Retrieve info
$result = mysql_query("SELECT brandid, productname, price, currency, recpriceretail, productnumber, productid FROM products WHERE username='$session->username' ORDER BY brandid") or die(mysql_error());

// Format of table
echo "<table border='1' bordercolor='#cccccc' cellspacing='0' cellpadding='3'>";
echo "<tr bgcolor='#cccccc'><td>Brand</td>
<td>Prod name</td><td>Prod number</td><td>Price</td><td>Retail Price</td> <td>Currency</td> <td>Update</td></tr>";

// get the rows from the query
while($row = mysql_fetch_array( $result ))
{
//print $results at end
$results .= "<tr>";
$results .= '<td>'.$row['brandid'].'</td>';
$results .= '<td>'.$row['productname'].'</td>';
$results .= '<td>'.$row['productnumber'].'</td>';
$results .= '<td>'.$row['price'].'</td>';
$results .= '<td>'.$row['recpriceretail'].'</td>';
$results .= '<td>'.$row['currency'].'</td>';
$results .= '<td><a href="prodinfo.php?productid='.$row['productid'].'">Upd</a></td>';
$results .= "</tr>";
}
echo $results;
echo "</table>";
?>

thetestingsite
03-06-2008, 08:15 PM
Taken from your first post:



<div id="content">

<h2>Products</h2>

<?php
// Retrieve info
$result = mysql_query("SELECT brandid, productname, price, currency, recpriceretail, productnumber, productid FROM products WHERE username='$session->username'") or die(mysql_error());

// Format of table
?>
<table border='1' bordercolor='#cccccc' cellspacing='0' cellpadding='3'>
<tr bgcolor='#cccccc'>
<td>Brand</td>
<td>Prod name</td>
<td>Price</td>
<td>Currency</td>
<td>Retail Price</td>
<td>Prod number</td>
<td>Update</td>
</tr>
<?php
// get the rows from the query

while($row = mysql_fetch_array($result)) {
// print the rows
?>
<tr>
<td><?php echo $row['brandid']; ?></td>
<td><?php echo $row['productname']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['currency']; ?></td>
<td><?php echo $row['recpriceretail']; ?></td>
<td><?php echo $row['productnumber']; ?></td>
<td><a href="userinfo.php?productid=<?php echo $row['productid'];?>">Upd</a></td>
</tr>
<?php
}
?>
</table>
</div>


You really should get out of php parsing before outputting html (like my above example). It is easy to troubleshoot when there are errors and you do not end up making simple mistakes such as forgetting to escape slashes or whatnot.

Hope this helps.

city_coder
03-06-2008, 10:33 PM
Surely if you stop and start the PHP engine (<? & ?>) then it will slow down the loading of the page.
Better to do it all in one go, granted that this can get messy. Although if your only at developing and testing then it is a good way to solve simple mistakes.

thetestingsite
03-06-2008, 10:39 PM
Actually, passing html code through the php parser makes it run slower (even if it is only fractions of a second). The way shown above is the preferred method of outputting html to the browser through php.

Hope this helps.