Solved

php loops

Posted on 2013-06-12
9
180 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
  • 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
 

Author Comment

by:doctorbill
ID: 39242315
I wanted the results echoed in the body section as a list
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP script redirecting to URL not working on AWS EC2 after Elastic IP 12 67
Load google maps api into wordpress 5 37
get domain with php 7 20
hosting images 4 12
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now