Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to assign a different variable name to each loop?

Posted on 2012-04-04
2
Medium Priority
?
282 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 2000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

718 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