1

I'm a fairly new programmer and I'm currently working on creating a program for a web-based art exhibit that would will display a random image and text using data from a PHP form which requires inputs for both.

I will need to create a PHP form that gathers text and a picture from the visitor. Example PHP page:

*What is your name?* = Max

*Upload photo.* = pic.jpg

The finished project would be a webpage that - on refresh - would display the image and the text gathered from the PHP form randomly. So on refresh the page would display a random text input (I.E Max, Alan, Mark, etc.) and a random picture (I.E pic.jpg, pic1.jpg, pic2.jpg) I'm guessing from a MySQL database.

The page, on refresh, would therefore display something like this:

Max / pic.jpg

REFRESH

Alan / pic3.jpg

REFRESH

Mark / pic2.jpg

And so on...

This is what I have so far:

    <?php 

 //This is the directory where images will be saved 
 $target = "images/"; 
 $target = $target . basename( $_FILES['photo']['name']); 

 //This gets all the other information from the form 
 $name=$_POST['name'];  
 $pic=($_FILES['photo']['name']); 

 // Connects to your Database 
 mysql_connect("mysite.com", "username", "password") or die(mysql_error()) ; 
 mysql_select_db("db_name") or die(mysql_error()) ; 

 //Writes the information to the database 
 mysql_query("INSERT INTO `employees` VALUES ('$name', '$pic')") ; 

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 
 ?>

Any help on this would be much appreciated!

Thank you!

mxmxwo
  • 31
  • 1
  • 10
  • There is 2 ways - `RAND` in `SQL` query or `rand` in `php` after you've extracted both name/pic data arrays from database. Yes, it should be in database. – Damaged Organic Dec 03 '13 at 21:45
  • Here's [php's `rand()` documentation](http://us3.php.net/rand) – Francisco Presencia Dec 03 '13 at 21:46
  • Thank you, @grimv01k . I assumed it should be kept in a database. What should the PHP form and HTML page look like code wise then? – mxmxwo Dec 03 '13 at 21:47
  • For the names, I'd go with `echo array_rand(['Mike', 'John', 'Peter']);` if it's a short list. If it's a long, you can either parse it from a file, include another file or fetch it from the database. Please update your question to reflect what you're looking for. – Francisco Presencia Dec 03 '13 at 21:47
  • Thank you, @FranciscoPresencia - how would I go about creating a PHP form that adds the uploaded picture and submitted text to that list? – mxmxwo Dec 03 '13 at 21:49
  • It depends, do you want it to be from ANOTHER visitor or would you write manually the names/pictures? – Francisco Presencia Dec 03 '13 at 21:51
  • @FranciscoPresencia I would like it to be live so if someone submits the PHP form (image and text) both of those are added to their respective lists automatically, not manually. – mxmxwo Dec 03 '13 at 21:52
  • Okay, then you need to go the database route... and that's definitely too long for a simple answer on SO. I'll explain the steps and refer to tutorials. – Francisco Presencia Dec 03 '13 at 21:53

2 Answers2

1

First you will need to create the html form. I assume you have some basic knowledge about this, so let's assume you do it and post it to post.php. Here you'll need to:

  • Handle the image.
  • Insert the name and image reference into the database.
  • Redirect the user to the landing page (important to avoid refreshing issues like "want to send your data again?").
  • Fetch a random entry for the database and display it to the user on the landing page.

A few tutorials:

However, it's not a 'trivial' task. It'd take few days to someone familiar with the languages, with someoone who is not maybe 1 week provided you start from the ground up.

Community
  • 1
  • 1
Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
1
Pull out the data using 

SELECT * FROM table ORDER BY RAND() LIMIT 0,1;

Then pull out the 

<?
$sql=mysql_query("SELECT * FROM table ORDER BY RAND() LIMIT 0,1;");

while($row=mysql_fetch_array($sql)){

    $name=$row['name'];
    $imagename=$row['imagename'];   


//write the logic here use echo or close and open php using <? and ?>
//giving an example

echo "Name: ".$name."<br>";
echo "<img src='directory/'".$imagename." width='100' height='100'>";

//OR other way

?>
Name : <? echo $name;?><br>

<img src="directory/<? echo $imagename;?>" width="100" height="100">


<?

}
?>