Solved

I'm having a little trouble with a default value in a custom function - could someone help?

Posted on 2011-02-20
9
276 Views
Last Modified: 2012-06-27
The function gets a testimonial from a table like this:

type,id,name,location,testimonial
'type1',1,'Mr A','Manchester','They were great!'
'type2',1,'Mrs B','Liverpool','They were smashing!'

NB There are only 3 of type2, but 5 of each of the others, hence the line starting $count.

I want to be able to get a testimonial either of a specific type, or at random:

getTestimonial(type1);     // Should return a testimonial of type1
getTestimonial();              // Should return a testimonial of any type

I've tried using Nulls & empty strings, but if I don't pass a type, then all I get back is:

<br />
~ ,

Can anyone see where I'm going wrong?
<?php
error_reporting(E_ALL);
function getTestimonial($type="")
{
	if (""==$type) {
		$types = array("type1","type2","type3","type4");
		$type = $types[rand(3)];
	}
//	echo "[".$type."]";
	$count = ($type == 'type2') ? 3 : 5;
	$sqltest = "SELECT * FROM testimonials WHERE type='$type' AND id=".rand(1,$count)." LIMIT 1";
	$restest = mysql_query($sqltest);
	$rowtest = mysql_fetch_assoc($restest);
	return $rowtest[testimonial]."<br /> ~ ".$rowtest[name].", ".$rowtest[location];
}
?>

Open in new window

0
Comment
Question by:PaulCutcliffe
  • 5
  • 4
9 Comments
 
LVL 4

Accepted Solution

by:
LAMASE earned 333 total points
ID: 34939290
When you put an array inside a string it will render as the string "Array" (try to output the query).
You can simply remove the condition where no set is passed.

However you complicated your life with your random selection, use

SELECT .... WHERE .... ORDER BY RAND() LIMIT 1

It will return a random element in the selected set.
0
 
LVL 4

Expert Comment

by:LAMASE
ID: 34939298
PS the right way to specity a set of values in the query is

$query = "SELECT ... WHERE field IN ('".implode("','", $array)."')";

Open in new window

Will generate

... WHERE field IN ('value1','value2','value3')

Open in new window

0
 
LVL 4

Expert Comment

by:LAMASE
ID: 34939333
I forgot to tell you to use quotes in

getTestimonial("type1");

Open in new window

0
 
LVL 4

Expert Comment

by:LAMASE
ID: 34939347
When you put an array inside a string it will render as the string "Array" (try to output the query).
Sorry, I read the code too quick... however you can solve with the previous hints.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:PaulCutcliffe
ID: 34939402
I had thought that perhaps I should have used quotes in my function call, but it seemed to work fine as it was.

I'm sorry though, other than that, you've totally lost me. :-(
0
 

Author Comment

by:PaulCutcliffe
ID: 34939425
I've just checked my code, & I have used quotes.
0
 
LVL 4

Expert Comment

by:LAMASE
ID: 34939430
Is the query correct? can you print it before executing?
0
 

Author Comment

by:PaulCutcliffe
ID: 34939482
It works if I supply a parameter, just not if I don't. I think it's something in the if block, or the handling of an empty string or Null, that isn't working.

I think I can use randomisation in the query as you suggest, then I don't have to worry about how many of each type there are. I had thought that could get complicated!

But I just need to able to pull out a random one of any type, & that currently doesn't work.
0
 

Author Comment

by:PaulCutcliffe
ID: 34939586
Aha, got it.

I've changed it now to this:

<?php
error_reporting(E_ALL);
function getTestimonial($type='')
{
	if (''==$type) {
		$sqltest = "SELECT * FROM testimonials ORDER BY RAND() LIMIT 1";
	} else {
		$sqltest = "SELECT * FROM testimonials WHERE type='$type' ORDER BY RAND() LIMIT 1";
	}
	$restest = mysql_query($sqltest);
	$rowtest = mysql_fetch_assoc($restest);
	return "[".$rowtest[type]."]<i>".$rowtest[testimonial]."</i><br /> ~ ".$rowtest[name].", ".$rowtest[location];
}
?>

Open in new window


Thanks. Works perfectly.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

863 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

27 Experts available now in Live!

Get 1:1 Help Now