Solved

Empty variable

Posted on 2012-03-27
10
250 Views
Last Modified: 2012-03-27
I'm trying to figure out why $line['total1'] is empty when echo'd back?

$query = "select author_name, count(*) as total, 1 as type
from `posts` 
where from_unixtime(post_date) >=" . "'" . $startdate . "'" .
"and from_unixtime(post_date) <" . "'" . $enddate . "'" . 
"and new_topic = 0
group by author_name
union
select starter_name, count(*) as total1, 2 as type
from `topics`
where from_unixtime(start_date) >=" . "'" . $startdate . "'" .
"and from_unixtime(start_date) <" . "'" . $enddate . "'" . 
"group by starter_name
order by total desc";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

//Create the result set
while ($line= mysql_fetch_array($result))
	 {
	echo "Username: " .  $line['author_name'];
	echo "</br>";
	echo "Threads: " .  $line['total1'];
	echo "</br>";
	echo "Posts: " .  $line['total'];
	echo "</br>";
	echo "Overall Points: " .  (($line['total'] * $posts) + ($line2['total1'] * $topics));
	echo "</br>";
	echo "</br>";
	echo $line['total1'];
	}

Open in new window

0
Comment
Question by:N R
  • 5
  • 5
10 Comments
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 37773586
select 10 as total
union
select 20 as total1

>>>>

total
10
20

the name from first query will be used to name columns... there wont be a new column...
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 37773601
and use

if $line['type']=="1" echo "Threads: " . $line['total'];
if $line['type']=="2" echo "Posts: " . $line['total'];

instead of line 21-22-23
0
 
LVL 11

Author Comment

by:N R
ID: 37773638
I tried, but I get the same number for posts and topics.

Code Changed:
$query = "select author_name, count(*) as total, 1 as type
from `posts` 
where from_unixtime(post_date) >=" . "'" . $startdate . "'" .
"and from_unixtime(post_date) <" . "'" . $enddate . "'" . 
"and new_topic = 0
group by author_name
union
select author_name, count(*) as total, 2 as type
from `posts` 
where from_unixtime(post_date) >=" . "'" . $startdate . "'" .
"and from_unixtime(post_date) <" . "'" . $enddate . "'" . 
"and new_topic = 1
group by author_name
order by total desc";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

//Create the result set
while ($line= mysql_fetch_array($result))
	 {
	echo "Username: " .  $line['author_name'];
	echo "</br>";
	if ($line['type'] = "2")
	echo "Threads: " .  $line['total'];
	echo "</br>";
	if ($line['type'] = "1")
	echo "Posts: " .  $line['total'];
	echo "</br>";
	echo "Overall Points: " .  (($line['total'] * $posts) + ($line2['total1'] * $topics));
	echo "</br>";
	echo "</br>";
	echo $line['total1'];;
	}

Open in new window

0
 
LVL 51

Expert Comment

by:HainKurt
ID: 37773687
you will get a result like

author_name total type
aaaa 100 1
bbbb 125 1
ccccc 256 2
dddd 342 2

Open in new window


first 2 will be posts, last 2 will be threads...

and I guess you should use == not =

if ($line['type'] == "1") echo "Posts: " .  $line['total'];
if ($line['type'] == "2") echo "Threads: " .  $line['total'];
echo "</br>";
0
 
LVL 11

Author Comment

by:N R
ID: 37773721
Strange, if I change to:

if ($line['type'] == "1")
	echo "Threads: " .  $line['total'];
	echo "</br>";
	if ($line['type'] == "2")
	echo "Posts: " .  $line['total'];

Open in new window


It ends up giving the the post count for threads and nothing for posts.
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 51

Expert Comment

by:HainKurt
ID: 37773755
what about this

$query = "select author_name, 
       sum(case when new_topic = 0 then 1 else 0 end) post_total,
       sum(case when new_topic = 1 then 1 else 0 end) thread_total
from `posts` 
where from_unixtime(post_date) >=" . "'" . $startdate . "'" .
"and from_unixtime(post_date) <" . "'" . $enddate . "'" . 
group by author_name
order by total desc";

with above query you will get

author_name post_total thread_total
HainKurt  234 512
SomeOne 102 253

Open in new window


one query returns all data...
0
 
LVL 11

Author Comment

by:N R
ID: 37773805
All that data looks good that it returns, how would I modify my "if" statements then?
0
 
LVL 11

Author Comment

by:N R
ID: 37773831
Ah it eliminates it, got it working, thanks for all the help!
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 37773944
I did not look at query closely at first, thought they are getting data from different tables :)
With new query, you will get all data in same row, so you dot need if in your code...
0
 
LVL 11

Author Comment

by:N R
ID: 37773949
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Echo vs ?><?php  html code 4 46
Session timeout 5 14
How can I echo a PHP variable in AJAX function? 7 35
Website Interactive tool that will do calculations 3 29
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

920 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

13 Experts available now in Live!

Get 1:1 Help Now