Solved

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

Posted on 2011-02-20
9
279 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

726 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