0

So, I have a MySQL query that returns a number of results (in this case, different currencies for products). They appear in the table like this:

Product Name  |  Euros  |  Dollars  |  GBP  |  Stock?
_______________________________________________
Acme Produce  |  34.00  |  52.00  |  30.00  |  "In Stock"
MoreProducts  | 153.00  | 160.00  | 144.00  |  "In Stock"

I want to be able to print it out in PHP all on one line (sounds silly when you have information in a table, I know, but there is a reason for it) like this

"You have a number of products including 'Acme Produce', currently selling for E34.00, $52.00, £30.00 in stock, and 'MoreProducts', currently selling for E153.00, $160.00, £144.00 in stock which you have not looked at for a while"

How do I possibly construct a string for this?!

I get the products from MySQL using the following:

$query_productDetails = sprintf("SELECT * FROM ProductsView")
$query_limit_productDetails = sprintf("%s LIMIT %d, %d", $query_productDetails, $startRow_productDetails, $maxRows_productDetails);
$productDetails = mysql_query($query_limit_productDetails, $Products) or die(mysql_error());
$row_productDetails = mysql_fetch_assoc($productDetails);

if (isset($_GET['totalRows_productDetails'])) {
  $totalRows_productDetails = $_GET['totalRows_productDetails'];
} else {
  $all_productDetails = mysql_query($query_productDetails);
  $totalRows_productDetails = mysql_num_rows($all_productDetails);
}

and then get the individual columns in a <?php echo $row_productDetails['GBP']; ?> (for example).

How do I get this information into an array, so that I can put it together in the desired format?

2
  • Off topic but is it wise to save the prices as fixed values for different currencies rather that using rates? Commented Mar 6, 2014 at 17:29
  • A good point - but there is a boring contractual reason for this method. Commented Mar 7, 2014 at 8:09

1 Answer 1

1

Can't you just do it like this:

$lines = array();
foreach ($row_productDetails) {
    $lines[] = "'{$row_productDetails['NAME']}', currently selling for E{$row_productDetails['EUR']}, ${$row_productDetails['USD']}, £{$row_productDetails['GBP']} {$row_productDetails['InStock']}";
}

var_dump(implode(", and "));
Sign up to request clarification or add additional context in comments.

3 Comments

Actually... that's pretty neat! I had been trying a bizarre series of implodes and explodes... but this is sweet! Thanks for the answer!
One more thing - there seems to be a syntax error on the second line - is there a missing bracket here?
It was written as example, you have to iterate thru your rows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.