Solved

How to assign a different variable name to each loop?

Posted on 2012-04-04
2
262 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
change password links 7 72
phpmyadmin 3 31
Access Object's Property 9 21
How do I execute a file from within a PHP script with headers already sent? 5 20
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

943 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

9 Experts available now in Live!

Get 1:1 Help Now