dynamic PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gunter
Forum Newbie
Posts: 9
Joined: Wed Aug 04, 2010 3:09 am

dynamic PHP

Post by gunter »

Hey, I have a piece of PHP code, which draws pieces of text from a database. Which is as follows:

Code: Select all

<?php
 
if($_GET['act'] == "generate_quotes") {

$db = mysql_connect(l*", "*", "*") or die ("Unable to connect to database.");
mysql_select_db("quote") or die ("Unable to select database.");


$result = mysql_query( " SELECT * FROM `quote`  ORDER BY RAND() LIMIT 0,1 " );
$fetch = mysql_fetch_array($result); 


echo "<blockquote>".$fetch['q_quote']."</blockquote>";
mysql_close($db);

} else { 

echo "<img src=\"1.png\">";

}
?>


<div id="gen"><a href='gengen.php?act=generate_quotes'>Generate</a></div>


This works fine. BUT i would like to change this code dynamically on the page. I've heard this is possible with just php, to avoid using javascript.

Could anyone tell me a good way of going about doing this?>

http://www.wickham43.net/showform.php?n ... phpnewsbox

I found this, which uses just PHP, but sadly this uses .inc files and not a databse.

thanks for any input.
Last edited by califdon on Wed Aug 11, 2010 7:42 pm, edited 1 time in total.
Reason: Changed code tag to syntax=php tag.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: dynamic PHP

Post by JakeJ »

Don't worry about the .inc file. You can put whatever content you want in there including database queries.

The file extension doesn't matter. When you include a file, it parses the content of the file, it doesn't care about the file name.

So go ahead and use the example you linked to; just put your database queries in side of that file and assign your variables there. It should work just fine.
gunter
Forum Newbie
Posts: 9
Joined: Wed Aug 04, 2010 3:09 am

Re: dynamic PHP

Post by gunter »

sorry to bump this but just got time to reply.

Thanks for the info about the .inc files. But could anyone answer these questions on why to use a .inc file.

Is a .inc file slower than a database? [the text it shall display shall be at least a paragraph long and at least 100 entires.]

And i need there to be a file assigned to each array, so the user can download the text as a PDF. Which is why I was using a database in the first place. Does anyone have any idea if this is possible with a .inc file?

example of what the .inc file may look like? [haven't tested this, just included it so people could maybe use it as a template if they have any idea how to go about doing the above.]

Code: Select all

 
$i=0;
$myDirectory = dir("some/dir");
while($file=$myDirectory->read())
{
$array[$i]=$file;
$i++;
}
$myDirecotry->close();

$num = count($array);
$random = rand(0, $num);

include "$array[$random]";
And maybe something like

Code: Select all

 $array = array();
$array[] 'fileone.txt';
$array[] 'filetwo.txt';


For the array. But like I said. There's going to be quite a few files being used here. So maybe this isn't the best option?

thanks for any advice.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: dynamic PHP

Post by JakeJ »

Most people don't use text pulled from a file. Though in many tutorials on line, the examples pull from a file because then they don't have to maintain a database. But in general, you should use a database.
gunter
Forum Newbie
Posts: 9
Joined: Wed Aug 04, 2010 3:09 am

Re: dynamic PHP

Post by gunter »

Thanks, but say I did have a database, and the whole page was a PHP document. Would it be possible to change the text dynamically without using AJAX or something similar?

E.g

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="shortcut icon" href="favicon.ico">        
                            
	<title>//</title>
                
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link REL="SHORTCUT ICON" HREF="img/favicon.ico">

</head>

<body id="home">

  <!--wrap-->
  
<div id="wrap">

      
 <?php
 
if($_POST['act'] == "generate_quotes") {

$db = mysql_connect("localhost", "root", "bonnie") or die ("Unable to connect to database.");
mysql_select_db("quote") or die ("Unable to select database.");


$result = mysql_query( " SELECT * FROM `quote`  ORDER BY RAND() LIMIT 0,1 " );
$fetch = mysql_fetch_array($result); 


echo "<blockquote>".$fetch['q_quote']."</blockquote>";
mysql_close($db);

} else { 

echo "<img src=\"1.png\">";

}
?>

<div id="gen"><a href='gengen.php?act=generate_quotes'>Generate</a></div>
 
<!--end wrap-->
</div>
<!--end wrap-->

</div>
    
</body>
</html>
Would anyone know how to make this change dynamically? I read somewhere you can use 'post' instead of 'get' or something along those lines. Anyone mind showing me how it could be done with this?
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: dynamic PHP

Post by JakeJ »

Change your form method to POST and then access all the field names of your form as $_POST['field1'] etc.

Then do you what you want with your code from there. There's much better examples in tutorials out there than I can find.

BUT.. if you truly want to make it dynamic, use jQuery and the form plug in. It's pretty painless and you don't have to reload the whole page when it's processed.

If you're not comfortable doing that though, just use the normal form methods.
gunter
Forum Newbie
Posts: 9
Joined: Wed Aug 04, 2010 3:09 am

Re: dynamic PHP

Post by gunter »

//EDIT

Code: Select all

 <?php
 
if($_POST['act'] == "generate_quotes") {

$db = mysql_connect("localhost", "root", "bonnie") or die ("Unable to connect to database.");
mysql_select_db("quote") or die ("Unable to select database.");


$result = mysql_query( " SELECT * FROM `quote`  ORDER BY RAND() LIMIT 0,1 " );
$fetch = mysql_fetch_array($result); 

 echo "Welcome ". $_POST['q_quote']. "<br />";
 
mysql_close($db);


} else { 

echo "<img src=\"1.png\">";

}
?>

<form action="gengen.php?act=generate_quotes" method="POST">

<input type="submit" />
</form>

Test: <?php echo $_POST["q_quote"]; ?>!<br />

I've had a go at what you suggested but still no luck, it's changing the url like it should, but i'm unsure how to display anything :?
Last edited by gunter on Fri Aug 13, 2010 7:49 am, edited 3 times in total.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: dynamic PHP

Post by JakeJ »

If you don't have a form, how are you using post.
Here's a form example.
This is a very simplistic form of course, modify it to suit your own needs.

Code: Select all

//page.php
<?php
If (isset($_POST['text'])) {
     Echo "Your Form Has Been Submitted";
}
?>
<html>
<form id="your_form" name="your_form" method="post" action=<?php $_SERVER['PHP_SELF']; ?> >
<input type="text" id="text" name="text" value="">
<input type="submit" value="Submit">
</form>
</html>


gunter
Forum Newbie
Posts: 9
Joined: Wed Aug 04, 2010 3:09 am

Re: dynamic PHP

Post by gunter »

Thanks, ill give that a go :)
Post Reply