[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

php loops

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
doctorbill
Asked:
doctorbill
  • 4
  • 3
  • 2
5 Solutions
 
GaryCommented:
$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
 
doctorbillAuthor Commented:
Is this in the top section of the page, above the head tag, or in the body where I can use echo commands ?
0
 
GaryCommented:
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!

 
doctorbillAuthor Commented:
I wanted the results echoed in the body section as a list
0
 
GaryCommented:
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
 
Ray PaseurCommented:
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
 
doctorbillAuthor Commented:
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
 
Ray PaseurCommented:
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
 
doctorbillAuthor Commented:
thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now