Solved

How to assign a different variable name to each loop?

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

758 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