• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

parsing php files into html?


bentzak

Senior Member
#1
Hi,

i am putting a little random quote generator built using php, into a page built using html and css. i have found links on the internet explaining how to do this, but it seems you need to know a bit more than i do to figure it out! can anyone help me? this web design just gets trickier and trickier i tell thee! also while im here, i want to do the same to build a contact page form, is that doable? or is there a far simper way to be doing this?

Cheers,

Zak
 

Harry

Senior Member
#2
I dunno what you mean. You want to output html code in a php file? There are several ways:

PHP:
<?php
  $variableName = 'foo';
?>

<?php
  if($variableName == 'foo'){
    echo "<p>bar</p>";
  }
?>

<?php
  if($variableName == 'foo'){
?>
<p>bar</p>
<?php
  }
?>
These both output:

HTML:
<p>bar</p>
Although I'm not 100% sure what you mean, like I said.

However, if by 'random quote generator' you mean every time you refresh the page you get a new quote then you might want something like this:

PHP:
<?php
	$quotes = array(
		array('quote' => 'quote one', 'name' => 'timmy'),
		array('quote' => 'quote two', 'name' => 'bob'),
		array('quote' => 'quote three', 'name' => 'allen')
	);
	shuffle($quotes);
?>
<blockquote>
	<p><?php echo $quotes[1]['quote']; ?> <strong><?php echo $quotes[1]['name']; ?></strong></p>
</blockquote>
EDIT:

I think I may have just wasted a lot of time there :(
More specifics please.
 

bentzak

Senior Member
#3
i think the opposite is what i want. i have already finished the page in hmtl, using css. so its all layed out with space to put he random quote bit in. i got the code for the generator which works fine in its own php file, but i dont know what i need to do so the php quotes apear on the html page,

i dunno if that makes sense? or if its possible!

Z
 

Harry

Senior Member
#4
Did you get the code as third party or was it written specifically for you? If it was third party then delete it all anyway. Use mine, that's free and I'm here to give support with it :D

Say your random quote is to appear in a page called about.html.

Simply change it to about.php, paste the above in where you'd like it to appear et voila. Job's a goodun.
 

bentzak

Senior Member
#7
ah, i wasnt running through a php server, just put it on there, but my whole layout has gone? all the content, but no layout???

Z
 

Harry

Senior Member
#11
That is weird. Are you running a local server? Are your links definitely pointing to the right place? That code won't affect anything to do with styling.
 

bentzak

Senior Member
#12
aha! could be on to something there! im doing it through xampp localy, do i ned to put my whole rouut folder into the htdocs folder? that would make sense! i will try it.

Z
 

Harry

Senior Member
#15
Save the code I gave you in a file called quotes.php in a folder called includes in your root. Wherever you'd like to use it on your site:

PHP:
<?php include("/includes/quotes.php"); ?>
 

bentzak

Senior Member
#17
@ mcb, im trying to do it with out using javascript, its more of an added feture really, so i dont want to loose accessibility.

@ harry, when i changed the quotes for the quote generator the whole page disapered and came up with this,

Parse error: syntax error, unexpected T_ARRAY, expecting ')' in /Applications/XAMPP/xamppfiles/htdocs/rootfolder web/home/index.php on line 186

i deleted the extra quotes i put in, and it worked again, what do i need to do to get it working with more??
any ideas?
also i dont know if my local server is set up right, but when i load my website it goes to a page that says, object not found, but if i then click on the localserver tab it goes to the folders, and i can get to the error above. how do i get it so it finds it?

Z
 

Harry

Senior Member
#18
It may because you're leaving a comma on your last quote array:

PHP:
<?php
    $quotes = array(
        array('quote' => 'quote one', 'name' => 'timmy'), //COMMA HERE
        array('quote' => 'quote two', 'name' => 'bob'), //COMMA HERE
        array('quote' => 'quote three', 'name' => 'allen') //NO COMMA HERE
    );
    shuffle($quotes);
?>
as for your other issue, I don't know as I can access your server :/
 

bentzak

Senior Member
#19
yep, thats that sorted, its all working fine, except for having to go through the localhost folders through my browser to get to any of my pages, once im there they open fine and link fine, but not sure why its doing it, oh well, cheers for that, appreciate you taking the time to reply! truly helpfull, and i have learned a fair bit as well! bonus,

Z