Solved

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

Posted on 2011-02-20
9
280 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
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 look for a specific file type in a local or remote server directory using PHP.

688 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