?
Solved

php loops

Posted on 2013-06-12
9
Medium Priority
?
188 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 1200 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 1200 total points
ID: 39242247
I don't know where you want it printing out.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1200 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

777 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