ukphoto Posted March 28, 2008 Share Posted March 28, 2008 The following code works perfectley OK but do I really need the 3 lines from $RealResult to $ExplodeArray or is there a simpler way of doing this? <?php include 'testconOrders.php'; $Cust_Name = $_POST['Customer_Name']; $Query=("SELECT `Array` FROM tbl_orders Where `CustomerName` = '$Cust_Name' "); $Result = mysql_query($Query)or die(mysql_error()); $RealResult = mysql_fetch_assoc($Result); $ResultString=$RealResult['Array']; $ExplodeArray = unserialize($ResultString); if (isset($ExplodeArray)){ echo'The images ordered for '.$Cust_Name.' are as follows;<br><br>'; foreach($ExplodeArray as $k => $v){ echo 'Image: '.$k.' Qty: '.$v.'<br>'; } } else echo'No order for '.$Cust_Name.' or nill order sent'; ?> Mnay thanks Quote Link to comment https://forums.phpfreaks.com/topic/98298-solved-can-this-code-be-optimised/ Share on other sites More sharing options...
rhodesa Posted March 28, 2008 Share Posted March 28, 2008 Here it is a little shorter, but there isn't much more you can do: <?php include 'testconOrders.php'; $Result = mysql_query("SELECT `Array` FROM tbl_orders Where `CustomerName` = '".mysql_real_escape_string($_POST['Customer_Name'])."' LIMIT 1") or die(mysql_error()); $Row = mysql_fetch_assoc($Result); if($ExplodeArray = unserialize($Row['Array'])){ echo "The images ordered for {$Row['CustomerName']} are as follows;<br><br>"; foreach($ExplodeArray as $k => $v) echo "Image: {$k} Qty: {$v}<br>"; }else echo "No order for {$_POST['Customer_Name']} or nill order sent"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98298-solved-can-this-code-be-optimised/#findComment-503000 Share on other sites More sharing options...
ukphoto Posted March 28, 2008 Author Share Posted March 28, 2008 Rhodesa, many thanks for that. There are lots of bits that I can see where I could improve my own coding. Mike Quote Link to comment https://forums.phpfreaks.com/topic/98298-solved-can-this-code-be-optimised/#findComment-503016 Share on other sites More sharing options...
rhodesa Posted March 28, 2008 Share Posted March 28, 2008 More important the shortening your code is making sure you use mysql_real_escape_string() on all User Submitted data that is going into an SQL query. It helps prevent people from submitting damaging code. Quote Link to comment https://forums.phpfreaks.com/topic/98298-solved-can-this-code-be-optimised/#findComment-503020 Share on other sites More sharing options...
ukphoto Posted March 28, 2008 Author Share Posted March 28, 2008 Rhodesa, I realised that I may have to look back over more than one record so have amended the code to list the orders from newest to oldest i.e DESC order. I have left out the LIMIT, would it have any use here now? <?php include 'testconOrders.php'; $Result=mysql_query("SELECT `Array`, `Index` FROM tbl_orders Where `CustomerName` = '".mysql_real_escape_string($_POST['Customer_Name'])."' ORDER BY `Index` DESC ") or die(mysql_error()); echo"The images ordered for {$_POST['Customer_Name']} are as follows;<br><br>"; print "<table border cellpadding=3>"; while ($Rows = mysql_fetch_array($Result)) if ($ExplodeArray=unserialize($Rows['Array'])){ print "<tr><th>Image:</th><th>Quantity:</th>"; foreach($ExplodeArray as $k => $v) print "<tr><td>Image: {$k}</td><td> Qty: {$v}</td></tr>"; } print "</table>" ?> Quote Link to comment https://forums.phpfreaks.com/topic/98298-solved-can-this-code-be-optimised/#findComment-503097 Share on other sites More sharing options...
rhodesa Posted March 28, 2008 Share Posted March 28, 2008 almost there...you need braces for your while loop. you can also leave Index out of the SELECT fields: <?php include 'testconOrders.php'; $Result=mysql_query("SELECT `Array` FROM tbl_orders Where `CustomerName` = '".mysql_real_escape_string($_POST['Customer_Name'])."' ORDER BY `Index` DESC ") or die(mysql_error()); if(mysql_num_rows($Result)){ echo"The images ordered for {$_POST['Customer_Name']} are as follows:<br><br>"; print "<table border cellpadding=3>"; while ($Row = mysql_fetch_array($Result)){ if ($ExplodeArray=unserialize($Row['Array'])){ print "<tr><th>Image:</th><th>Quantity:</th>"; foreach($ExplodeArray as $k => $v) print "<tr><td>Image: {$k}</td><td> Qty: {$v}</td></tr>"; } } print "</table>"; }else echo "Customer {$_POST['Customer_Name']} has no orders; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98298-solved-can-this-code-be-optimised/#findComment-503107 Share on other sites More sharing options...
ukphoto Posted March 28, 2008 Author Share Posted March 28, 2008 Aaron, thanks for the help, I find the more I do the less I seem to know at present. Lets see where I can get to now. mike Quote Link to comment https://forums.phpfreaks.com/topic/98298-solved-can-this-code-be-optimised/#findComment-503117 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.