Solved

How to assign a different variable name to each loop?

Posted on 2012-04-04
2
277 Views
Last Modified: 2012-04-04
Hi, below I have a query that when ran on my table produces the following correct result:
Phone 0,2,5
Text 2,4,3
Email 0,3,2

The series of 3 numbers represent how many of each type of call for the past 3 months.

Problem is, I can't for the life of me figure out how I can get unique variable names for the 3 series of numbers kind of like:

$name_array1 = $row["array"];

then next loop
$name_array2 = $row["array"];

and last loop
$name_array3 = $row["array"];

I need to be able to display the results in different areas of the page so I need to be able to echo out each one by itself. Make any sense??

$result = mysql_query("SELECT t.name, ifnull(c.array,'0, 0, 0') array from calltypes t left outer join (SELECT CallType, concat(sum(if(CallDate>='$threemonthsago' and CallDate<'$twomonthsago',1,0 and account_id = '$account_id')),', ', sum(if(CallDate>='$twomonthsago' and CallDate<'$onemonthago',1,0 and account_id = '$account_id')),', ', sum(if(CallDate>='$onemonthago' and CallDate<'$currentmonth',1,0 and account_id = '$account_id'))) array FROM callers WHERE CallDate >= '$threemonthsago' AND CallDate < '$currentmonth' GROUP BY CallType order by CallType) c on (t.id=c.CallType)");

while($row = mysql_fetch_array($result)){
$name = $row["name"];
$name_array = $row["array"];
echo $name." ".$name_array."<br />";
}

Open in new window

0
Comment
Question by:tjyoung
[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
2 Comments
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 37807091
The simplest way is to use the global variables array and append a serial number to the variable name:
$result = mysql_query("SELECT t.name, ifnull(c.array,'0, 0, 0') array from calltypes t left outer join (SELECT CallType, concat(sum(if(CallDate>='$threemonthsago' and CallDate<'$twomonthsago',1,0 and account_id = '$account_id')),', ', sum(if(CallDate>='$twomonthsago' and CallDate<'$onemonthago',1,0 and account_id = '$account_id')),', ', sum(if(CallDate>='$onemonthago' and CallDate<'$currentmonth',1,0 and account_id = '$account_id'))) array FROM callers WHERE CallDate >= '$threemonthsago' AND CallDate < '$currentmonth' GROUP BY CallType order by CallType) c on (t.id=c.CallType)");

$i = 1;
while($row = mysql_fetch_array($result)){
$_GLOBALS['name'.$i] = $row["name"];
$_GLOBALS['name_array'.$i] = $row["array"];
echo $_GLOBALS['name'.$i]." ".$_GLOBALS['name_array'.$i]."<br />";
$i++;
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 37807129
Thank God... works perfect.
Much appreciated! Never would have figured that out.
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…

751 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