Jump to content
phppup

upgrade OR implimentation problem

Recommended Posts

I am UPGRADING some old files to MySQLi and while doing so have decided to complete some leftover projects.

In this project, I was counting vegetables sold to customers that displayed a report displaying information like this:

There were 3 orders for carrots for 1 lbs. each
There were 2 orders for carrots for 2 lbs. each
There were 1 orders for carrots for 3 lbs. each

The code I used to generate the messages was:

while($row = mysqli_fetch_array($result)){
	echo "There were ". $row['COUNT(carrots)'] ." orders for carrots for ". $row['carrots'] ." lbs. each <br />";
}

At the time, I was going to call a separate script for carrots, potatoes, and celery.

Now, with my worldly advancement (or maybe not.. LOL) I realize I should be able to use the same single script and assign a variable to accommodate each vegetable item.

After declaring the $item = 'carrots'   I inserted it to test my theory.  I tested the output in stages and all seemed okay until I did this:

while($row = mysqli_fetch_array($result)){
	echo "There were ". $row['COUNT($item)'] ." orders for carrots for ". $row['$item'] ." lbs. each <br />";
}

Is there an issue with using a variable inside a $row?  Is there a syntax error (no error messages)?  Perhaps a MySQLi addition that is required?

Share this post


Link to post
Share on other sites

What is your code for querying your data and producing the values you want to output?

Share this post


Link to post
Share on other sites

It was:

$query = "SELECT carrots, COUNT(carrots) FROM $mytable WHERE carrots>0 GROUP BY carrots ORDER BY carrots ASC"; 
	 
$result = mysqli_query($link, $query) or die(mysqli_error());

Until I changed all the CARROTS to $item.

The loss of values did not occur until I altered the while statement.

Share this post


Link to post
Share on other sites

Now it's even more confusing. What does your data look like? - it's hard to build a picture from what you've given us.

Share this post


Link to post
Share on other sites
Posted (edited)

Note that within single quotes, PHP does not do substitution.

echo "There were ". $row[COUNT($item)] ." orders for carrots for ". $row[$item] ." lbs. each <br />";

 

Edited by gw1500se

Share this post


Link to post
Share on other sites

The data is in $mytable input into a column named carrots.

My output was

There were 3 orders for carrots for 1 lbs. each
There were 2 orders for carrots for 2 lbs. each
There were 1 orders for carrots for 3 lbs. each

but after altering the WHILE STATEMENT it became

There were  orders for carrots for  lbs. each
There were  orders for carrots for  lbs. each
There were  orders for carrots for  lbs. each

which removed the data information.

Does something special or unique to MySQLi need to be done to change the hardcoded word "carrots" to the variable $item within that statement?

Share this post


Link to post
Share on other sites

If you won't answer my question, why should anyone answer yours?

Share this post


Link to post
Share on other sites

Just what I would expect from the echo using those single quotes.

Share this post


Link to post
Share on other sites

The data is in a simple table with a column for carrots which received a numeric value of the amount that was ordered.  The table is populated by a form submission.

I think that is what you are asking me.

I do not have access to my code right now, but will try altering the quotes suggested by gw1500se.

Any additional guidance is welcome.

Share this post


Link to post
Share on other sites

I think we would like to see your EXACT table definitions so we can see how you are storing things.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • 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.