Solved

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

Posted on 2011-02-20
9
275 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 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

11 Experts available now in Live!

Get 1:1 Help Now