Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP XML Problem =)

Featured Replies

Okay, so I have a footer on my portfolio that I want to use to display a random user testimonial on page refresh.

 

I think the best way to do this would probably to use XML to create a list of stuff so that one entry will allow me to input the Quote, Client Name, and Client Site.

 

I then want PHP to dynamically choose a random entry in the XML file, and output the three values for the entry in their correct place in the php.

 

Is this possible, and if so how, as I've had great trouble researching this already.

 

Edit

 

Okay so lets say I have this XML file

 

<footer>
<testimonial>
 <quote>Quote Test 2</quote>
 <name>Client 2</name>
 <url>URL 2</url>
</testimonial>
<testimonial>
 <quote>Quote text 1</quote>
 <name>Client 1</name>
 <url>URL 1</url>
</testimonial>
</footer>

 

How could I use php to randomly insert it into html like this?

 

<div id="footer">
	<h2>Client Testimonials</h2>
	<blockquote><p><span class="bqstart">“</span>TESTIMONIAL QUOTE IN HERE<span class="bqend">”</span></p></blockquote>
	<p class="client">CLIENTS NAME >> CLIENTS URL</p>

</div>

Assuming you have access to SimpleXML on your server (you probably will have), I would do it like this:

<?php
//Load the xml file
$xml = simplexml_load_file('test.xml');

//create an empty array for the testimonials
$testimonials = array();

//loop through the testimonial elements in the xml file
//adding each one as an array inside the $testimonials array
foreach($xml->testimonial as $testi) 
{
	$quote = (string)$testi->quote;
	$name = (string)$testi->name;
$url = (string)$testi->url;
$testimonials[] = array('quote' => $quote,'name' => $name,'url' => $url);		
}

//select a random testimonial from the $testimonials array
$randTest = rand(0,count($testimonials)-1);

//assign variables for each child element of the selected testimonial
$selectedQuote = $testimonials[$randTest]['quote'];
$selectedName = $testimonials[$randTest]['name'];
$selectedUrl = $testimonials[$randTest]['url'];
?>

You can then echo the variables where needed:

<p class="client"><?php echo $selectedName; ?></p>

If you want to make sure that a different testimonial is displayed on every page load, you could store the current testimonial in a session and check the value each page load. If you need any help, just ask :)

  • Author

Cheers man, got that working, but yes, I would like to add in the storing of the current session to stop it repeating randomly, but have no clue how to impliment. This is way past my php level of knowledge currently.

 

Would you possibly be able to help? =)

Hey, no problem.

If you change the previous code to the following:

<?php
//Load the xml file
$xml = simplexml_load_file('test.xml');

//create an empty array for the testimonials
$testimonials = array();

//loop through the testimonial elements in the xml file
//adding each one as an array inside the $testimonials array
foreach($xml->testimonial as $testi) 
{
	$quote = (string)$testi->quote;
	$name = (string)$testi->name;
	$url = (string)$testi->url;
	$testimonials[] = array('quote' => $quote,'name' => $name,'url' => $url);		
}

//select a random testimonial from the $testimonials array
$newrandTest = rand(0,count($testimonials)-1);

//check for stored session testimonial
if (isset($_SESSION['randTest'])) {
	//if it exists, assign its value to $oldrandTest
	$oldrandTest = $_SESSION['randTest'];
} //else $oldrandTest is null
else {
	$oldrandTest = null;
}
//compare $oldrandTest and $newrandTest. If they are the same, move up or down the
//testimonials array to select a different testimonial
if ($oldrandTest == $newrandTest) {
	//select a new testimonial by moving up or down the array
		//depending on the current testimonials position
	if ($newrandTest < count($testimonials)-1 ) {
		$newrandTest += 1;
	} 
	elseif ($newrandTest > 0) {
		$newrandTest -= 1;
	}
}

//assign the $_SESSION['randTest'] the value of $newrandTest
$_SESSION['randTest'] = $newrandTest;

//assign variables for each child element of the selected testimonial
$selectedQuote = $testimonials[$newrandTest]['quote'];
$selectedName = $testimonials[$newrandTest]['name'];
$selectedUrl = $testimonials[$newrandTest]['url'];
?>

Hopefully the comments in the code explain everything that's happening.

Also, at the top of each page, you'll need to start the session:

<?php
session_start();
?>

How many testimonials have you got in the xml file?

Obviously, the more, the better as even now you might get 'testimonial 2' followed by 'testimonial 3' followed by 'testimonial 2' again. It might be better to just loop trough the $testimonials array (assigning the current one to the session variable and incrementing by one each time until the end of the array, then repeating.

It all depends how many testimonials you have.

Anyway, if none of it makes sense, ask again :)

  • Author

Hmm, that works, (I would have never got that far xD), but like you say, maybe looping through the array is better. It means that they will see every testimonial, and not see any repeats.

 

I know how the logic of looping through the array would work, but dont know enough php to know how to do it. =S

 

I really appreciate your help on this mate.

 

Cheers

Looping through the testimonials is a bit simpler.

Once again, change the previous code to this:

<?php
//Load the xml file
$xml = simplexml_load_file('test.xml');

//create an empty array for the testimonials
$testimonials = array();


//loop through the testimonial elements in the xml file
//adding each one as an array inside the $testimonials array
foreach($xml->testimonial as $testi) 
{
	$quote = (string)$testi->quote;
	$name = (string)$testi->name;
	$url = (string)$testi->url;
	$testimonials[] = array('quote' => $quote,'name' => $name,'url' => $url);		
}

//check for stored session testimonial
if (isset($_SESSION['randTest'])) {
	//has the page been seen before?
	$randTest = $_SESSION['randTest'];
} //if not, it's the first time, show the first testimonial
else {
	$randTest = 0;
}
//if the current testimonial is the last one, start from the beginning
//otherwise increment
if ($randTest >= count($testimonials)-1) {
		$randTest = 0;
	} 
	else {
		$randTest += 1;
	}

//assign the $_SESSION['randTest'] the value of $newrandTest
$_SESSION['randTest'] = $randTest;

//assign variables for each child element of the selected testimonial
$selectedQuote = $testimonials[$randTest]['quote'];
$selectedName = $testimonials[$randTest]['name'];
$selectedUrl = $testimonials[$randTest]['url'];
?>

It should now loop through the testimonials sequentially.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.