Solved

Empty variable

Posted on 2012-03-27
10
249 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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
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…
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

17 Experts available now in Live!

Get 1:1 Help Now