Diether Posted March 11, 2013 Share Posted March 11, 2013 (edited) Hi guys Good day, this codes works perfectly for me. <?php // This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM product ORDER BY date_added DESC"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["name"]; $details = $row["details"]; $size = $row["size"]; $price = $row["price"]; $quantity = $row["quantity"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $product_list .= "Product ID: $id - <strong>$product_name</strong> - Php$price - <em>Added $date_added</em> <a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; } } else { $product_list = "You have no products listed in your store yet"; } ?> My problem is i want to refactor this part and put it into table in order to give a pleasant look: $product_list .= "Product ID: $id - <strong>$product_name</strong> - Php$price - <em>Added $date_added</em> <a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; My Screenshots for my desired output.Here is what i did to refactor but it did not give me a good result: $product_list .= "?> <html> <table > <tr> <td> Product ID: <?php echo $id ?> </td> <td> <?php echo $product_name; ?> </td> <td> Php <?php echo $price; ?> </td> <td> Added <?php echo $date_added; ?> </td> <td> <?php echo <a href='inventory_edit.php?pid=$id'>edit</a> • ?> </td> <td> <?php echo <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; ?> </td> </tr> </table> pls help me to solve my desired output,, thanks Edited March 11, 2013 by Diether Quote Link to comment https://forums.phpfreaks.com/topic/275518-helprefactoring-php-codes/ Share on other sites More sharing options...
AyKay47 Posted March 11, 2013 Share Posted March 11, 2013 This is pseudo code to get you started with the basic structure. <table> <?php while($row = mysql_fetch_array($result)) { echo "<tr><td>something</td><td>something else</td></tr>"; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/275518-helprefactoring-php-codes/#findComment-1418002 Share on other sites More sharing options...
Diether Posted March 11, 2013 Author Share Posted March 11, 2013 (edited) This is pseudo code to get you started with the basic structure. <table> <?php while($row = mysql_fetch_array($result)) { echo "<tr><td>something</td><td>something else</td></tr>"; } ?> </table> this code does not follow my desired output. any solution guys.? please help me to solve this Edited March 11, 2013 by Diether Quote Link to comment https://forums.phpfreaks.com/topic/275518-helprefactoring-php-codes/#findComment-1418003 Share on other sites More sharing options...
ignace Posted March 11, 2013 Share Posted March 11, 2013 Look at what you wrote, and THINK why it may not work. Hint: <?php $hello = 'world'; echo "<?php echo $hello; ?>"; // Output: <?php echo world; ?> ?> Quote Link to comment https://forums.phpfreaks.com/topic/275518-helprefactoring-php-codes/#findComment-1418015 Share on other sites More sharing options...
AyKay47 Posted March 11, 2013 Share Posted March 11, 2013 (edited) this code does not follow my desired output. any solution guys.? please help me to solve this You're right it does't, because it was meant to point out a few things that you are doing incorrectly. 1. Your attempt will output an entire html page/table every iteration; you want to generate rows within the table, thus the while loop belongs inside of the table 2. As Ignace pointed out, you are including <?php tags and functions inside of an echo statement, this is incorrect and will output the literal string "<?php echo" Edited March 11, 2013 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/275518-helprefactoring-php-codes/#findComment-1418019 Share on other sites More sharing options...
Diether Posted March 12, 2013 Author Share Posted March 12, 2013 Look at what you wrote, and THINK why it may not work. Hint: <?php $hello = 'world'; echo "<?php echo $hello; ?>"; // Output: <?php echo world; ?> ?> what line are you pertaining that it gives me this kind of result? Quote Link to comment https://forums.phpfreaks.com/topic/275518-helprefactoring-php-codes/#findComment-1418104 Share on other sites More sharing options...
ignace Posted March 12, 2013 Share Posted March 12, 2013 Look at your code. And read what AyKay47 said. Quote Link to comment https://forums.phpfreaks.com/topic/275518-helprefactoring-php-codes/#findComment-1418118 Share on other sites More sharing options...
Diether Posted March 12, 2013 Author Share Posted March 12, 2013 <?php // This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM product ORDER BY date_added DESC"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { $product_list .= "<table><tr><td>Product ID</td><td>Items</td><td>Price</td><td>Date</td><td>Action</td></tr>"; while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["name"]; $details = $row["details"]; $size = $row["size"]; $price = $row["price"]; $quantity = $row["quantity"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $product_list .= "<tr><td>$id</td> <td><strong>$product_name</strong></td> <td>Php$price</td> <td><em>$date_added</em></td> <td><a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br /></td></tr>"; } } else { $product_list = "<tr><td colspan=5>You have no products listed in your store yet</td></tr>"; } ?> this solve my problem a while ago.. hope it will help a beginner like me Quote Link to comment https://forums.phpfreaks.com/topic/275518-helprefactoring-php-codes/#findComment-1418142 Share on other sites More sharing options...
AyKay47 Posted March 12, 2013 Share Posted March 12, 2013 Remember a closing </table> tag. Quote Link to comment https://forums.phpfreaks.com/topic/275518-helprefactoring-php-codes/#findComment-1418146 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.