Solved

php loops

Posted on 2013-06-12
9
185 Views
Last Modified: 2013-06-18
On the attached page I have the following recordset:
record_set('artGallery',"SELECT * FROM art_gallery WHERE artgal_Name = '$art_nme' ");

I am trying to get the following code to iterate through the array such that the "$sum_total" and "$sum_total2" produce the product of the two numbers in rows 1,2 3 etc as a list
This needs to be echoed in the body

I presume I need some type of loop ?
test.txt
0
Comment
Question by:doctorbill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 300 total points
ID: 39242122
$rows = mysql_num_rows($artGallery);

if ($rows > 0) {
	$sum_total=0;
	$sum_total2=0;

	while ($rowData = mysql_fetch_assoc($rows)) {
		$first_number = $row_artGallery[artgal_sp];
		$second_number = "2.54";
		$sum_total += $second_number * $first_number;
		$third_number = $row_artGallery[artgal_sl];
		$forth_number = "2.54";
		$sum_total2 += $forth_number * $third_number;

	}
}

print ($sum_total);
print ($sum_total2);

Open in new window

0
 

Author Comment

by:doctorbill
ID: 39242201
Is this in the top section of the page, above the head tag, or in the body where I can use echo commands ?
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 300 total points
ID: 39242247
I don't know where you want it printing out.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:doctorbill
ID: 39242315
I wanted the results echoed in the body section as a list
0
 
LVL 58

Accepted Solution

by:
Gary earned 300 total points
ID: 39242346
Place this in the body wherever you need it displayed

$rows = mysql_num_rows($artGallery);

if ($rows > 0) {
	while ($rowData = mysql_fetch_assoc($rows)) {
		$first_number = $row_artGallery[artgal_sp];
		$second_number = "2.54";
		$sum_total = $second_number * $first_number;
		$third_number = $row_artGallery[artgal_sl];
		$forth_number = "2.54";
		$sum_total2 = $forth_number * $third_number;
		print ($sum_total);
		print ($sum_total2); 
	}
}

Open in new window

0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 39242707
Consider these good learning resources for PHP and MySQL.  The professional approach might be to write a query that will aggregate and sum the data.  In any case, SELECT * would not be the right approach if you can find out the names of the columns.  You would want to select by column name instead.

Beginner: Yank Ignore the hokey title -- its a good book.
Beginner to intermediate: Welling/Thompson
Intermediate to Advanced: Powers
Advanced: Zandstra
0
 

Author Comment

by:doctorbill
ID: 39243687
I am getting this:

PHP Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in I:\lbcLive\artistInfo.php on line 53
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 39244567
That usually means your query failed and returned FALSE instead of a MySQL result resource.  This article shows how to run queries and check for success or failure.  Check the code samples - they are well-commented.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Closing Comment

by:doctorbill
ID: 39255844
thanks
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question