LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-16-2009, 03:19 AM   #1
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Angry Variables in PHP/MySQL


Let's say I have something like this:
PHP Code:
while($row mysql_fetch_array($Query))
   {
      
$Foo $Bar;
   } 
Furthermore, let's say that I want $Foo to end up with the product of the multiplication of two fields in the row, Price and Quantity. I could do this by replacing $Bar like:
PHP Code:
while($row mysql_fetch_array($Query))
   {
      
$Foo $row['Price']*$row['Quantity'];
   } 
However, due to some weird coding I did a while back, I am currently in a situation where I need for this computation (not the product) to be stored in $Bar outside the while loop, and then have $Foo end up with the product of the computation when being assigned the value of $Bar. I can't figure out how to do this and would really appreciate your help.

I've tried doing it like:
PHP Code:
$Bar "\$row['Price']*\$row['Quantity']";

while(
$row mysql_fetch_array($Query))
   {
      
$Foo $Bar;
   } 
But $Foo ends up with the text of my intended computation, instead of the product being computed inside the loop.

Last edited by win32sux; 02-16-2009 at 03:42 AM.
 
Old 02-16-2009, 04:48 AM   #2
rsciw
Member
 
Registered: Jan 2009
Location: Essex (UK)
Distribution: Home: Debian/Ubuntu, Work: Ubuntu
Posts: 206

Rep: Reputation: 44
What's wrong with the 2nd of the three code blocks?
if the coding is weird, it calls for a recoding

you are effectively defining $bar as a variable containing a string, and then assigning that
string to $foo

therefore you'll end up with
$foo == "\$row['Price']*\$row['Quantity']" every time.

might be doable with
sprintf
havn't tried that yet though in a circumstance like this O_o

overall it looks rather weird, as you say, and not neat.
Best way would be to do it like in the 2nd code block (where btw $foo gets overwritten every time if there's more than one data row, unless it's intended)
 
Old 02-16-2009, 04:55 AM   #3
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by rsciw View Post
What's wrong with the 2nd of the three code blocks?
if the coding is weird, it calls for a recoding

you are effectively defining $bar as a variable containing a string, and then assigning that
string to $foo

therefore you'll end up with
$foo == "\$row['Price']*\$row['Quantity']" every time.

might be doable with
sprintf
havn't tried that yet though in a circumstance like this O_o

overall it looks rather weird, as you say, and not neat.
Best way would be to do it like in the 2nd code block (where btw $foo gets overwritten every time if there's more than one data row, unless it's intended)
Yeah, I think you're absolutely right. The best option really is to rewrite this chunk of code in such a manner that this kind of weirdness is no longer necessary. I think I'll do that. Thanks man.
 
Old 02-16-2009, 04:59 AM   #4
rsciw
Member
 
Registered: Jan 2009
Location: Essex (UK)
Distribution: Home: Debian/Ubuntu, Work: Ubuntu
Posts: 206

Rep: Reputation: 44
yw
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Are PHP session variables held in memory when you leave a PHP site? Locura Programming 11 11-16-2008 08:37 PM
PHP/MySQL - Proper syntax for using alphabetical variables in a query? win32sux Programming 3 03-10-2008 06:37 AM
Adding users with PHP (pass php variables to Expect script) Jayla Programming 1 10-20-2006 10:44 AM
PHP:Unable to post variables from html form to mysql database vikram_cvk Linux - Software 1 09-29-2004 03:01 PM
Apache Mysql Php: mysql with php doesn't work breakerfall Linux - Networking 6 12-27-2003 08:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration