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
Solved

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

Posted on 2011-02-20
9
278 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

807 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