Jump to content

[SOLVED] Can this code be optimised?


ukphoto

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/98298-solved-can-this-code-be-optimised/
Share on other sites

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";
?>

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>"
?>

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;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.