PHP - Adding Additional String Or Caption To Random Images.
Hello everyone, I have this very short script I wrote with help from a book using arrays and the rand function. Basically it changes the images randomly on browser load or refresh. However, I am trying to add an additional, not sure what to call it, a string or another caption for each images. Instead of this being a caption, it will be a link, so a link will appear under each caption that goes to another page when it is clicked on. I tried adding another caption and adding a like to it but getting syntax error. Thanks everyone! Here is the code: Code: [Select] <?php $images = array( array('file' => 'image1', 'caption' => 'Caption 1'), array('file' => 'image2', 'caption' => 'Caption 2'), ); $i = rand(0, count($images)-1); $selectedImage = "graphics/{$images[$i]['file']}.png"; $caption = $images[$i]['caption']; ?> And here is where the images are written to the page: Code: [Select] <div id="stage"> <img src="<?php echo $selectedImage; ?>" alt="Random image" /> <p id="caption"><?php echo $caption; ?></p> </div> IC Similar TutorialsI want people to be able to post images on to my page but i want them to be able to add a caption when they post their image
This is the format I want the image and caption to be posted like
<div class="spacer"> </div> <div class="jokestary-img" title="Siri will screw up your relationship"> <img src="uploads/Siri will screw up your relationship.jpg" /> <div class="caption"><div>Siri will screw up your relationship.</div></div></div>The code for the upload form also wont work The page says "Upload complete!" before i even choose a file and doesnt send to my database ?php // properties of the uploaded file $name = $_FILES ["myfile"] ["name"]; $type = $_FILES ["myfile"] ["type"]; $size = $_FILES ["myfile"] ["size"]; $temp = $_FILES ["myfile"] ["tmp_name"]; $error= $_FILES ["myfile"] ["error"]; if ($error > 0) die ("Error uploading file! code $error."); else { if ($type== "video/av/png || $size 1000000") //conditions for file { die("That format is not allowed or file size too big"); } else move_uploaded_file($temp,"uploads/".$name); echo "Upload complete!"; } ?> Hi, I'm having trouble generating random string with 8 characters, when I refresh I sometimes get 6 characters and sometimes 8. "-" not included as character. Wrong: 6JW-F7W Correct: (should always generate random like this) DVMF-F36V I know I'm trying to achieve this in wrong methods but it's the only way I know. Hope someone can help. function getReference() { $length = 8; $characters = "0123456789abcdefghijklmnopqrstuvwxyz"; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, strlen($characters))]; } return strtoupper(substr($string, 0, -$length/2)."-".substr($string, $length/2-1, -1)); } In the PHP script I'm using, in the Upload Form the user selects an image to Upload, the Form renames it like so:
$allowedExts = array("gif", "jpeg", "jpg", "pdf", "png"); $temp = explode(".", $_FILES["file"]["name"]); $extension = strtolower( end($temp) ); if (!in_array($extension,$allowedExts)) { echo ("Error - Invalid File Name"); } $length = 20; $randomString = (time()); $thumbnail = $randomString . "." . $extension;The random string works successfully, but I'd like to add the user_id to the beginning of it and a dash, like this: user_id - So, the new file name would be something like: user_id-randomString.extension Can you please help me add that? i have to tables... forums & subscription i would like to insert a random character string into 'forums' to be used as a subscription id. and then have the same random string to be inserted into 'subscription' Hey all, I'm looking to produce a random 10 character string like: abcABC0123 when the below method is called: <?php function randomize(){ $chars = explode(' ',(range("a","z"))) . explode(' ',(range("A","Z"))) . explode(' ',(range("0","9"))); $pass = ""; for($i = 0; $i < $len; $i += 1){ array_push($pass,$chars[rand(count($chars)-1)]); } return $pass; } var_dump(randomize(10)); ?> But the randomize method is not returning desired result. Any idea what I'm doing wrong? Thanks for response. I'm looking for a function that creates a random string that contains random letters, numbers and symbols. Also it needs to generate it in a way that there are no repeating characters. Any ideas? So I found this code from someone else on here who posted about creating a random string. I've copied the code for myself but when I upload php page to my server and view source... there is no php code. Essentially, i see no randomly generated text. what am i missing? Code: [Select] <?php function genRandomString() { $length = 5; $characters = array_merge(range(0,9),range('a','z'),range('A','Z')); $string = ""; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, count($characters)-1)]; } return $string; } ?> I have 3 columns in my DB: email (primary), unique_code, timestamp Users enter their email address and it is added to the DB, including an alphanumeric 5 digit unique code in the 'unique_code' column and a timestamp in the 'timestamp' column. 1. The email address is being added, but not the unique code? What is the issue here and what can I should I specifically do to fix it? 2. The next thing is that I need to display that code to the user in where it says <?php echo $unique_code;?>. How can I do that? DB is as follows: Field Type Collation Attributes Null Default Extra Action email varchar(64) utf8_unicode_ci No None unique_code varchar(64) utf8_unicode_ci No None timestamp timestamp on update CURRENT_TIMESTAMP No CURRENT_TIMESTAMP INDEXES PRIMARY BTREE Yes No email 7 A Full code Code: [Select] <?php require "includes/connect.php"; $msg = ''; if($_POST['email']){ // Requested with AJAX: $ajax = ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); try{ if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){ throw new Exception('Invalid Email!'); } $mysqli->query("INSERT INTO coming_soon_emails SET email='".$mysqli->real_escape_string($_POST['email'])."'"); if($mysqli->affected_rows != 1){ throw new Exception('This email already exists in the database.'); } if($ajax){ die('{"status":1}'); } $msg = "Thank you!"; $mysqli->query("INSERT INTO coming_soon_emails VALUES('email', SUBSTRING(MD5(UUID()),FLOOR(RAND()*25),5), UNIX_TIMESTAMP())"); echo "Something went wrong:" . $mysqli->error; } catch (Exception $e){ if($ajax){ die(json_encode(array('error'=>$e->getMessage()))); } $msg = $e->getMessage(); } } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>example</title> <link rel="stylesheet" type="text/css" href="css/styles.css" /> </head> <body> <div id="container"> <form id="form" method="post" action=""> <input type="text" id="email" name="email" value="<?php echo $msg?>" /> <input type="submit" value="Submit" id="submitButton" /> </form> <div id="thankyou"> Thank you! <?php echo $unique_code;?></p> </div> </div> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script> <script src="js/script.js"></script> </body> </html> Thank you! Hi everyone Well I've started learning php yesterday and now I encountered my first problem, I looked around but I don't really know what to search for if I want my answer so here I come Code: [Select] <html> <body> <?php $names = array('Peter','Quagmire','Joe'); $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); $who = rand(0,2); $who2 = $names[$who]; echo "$names[$who] is $ages[$who2] years old"; ?> </body> </html> This is the script I have, and there is the kind of script I expected: Code: [Select] <html> <body> <?php $names = array('Peter','Quagmire','Joe'); $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); $who = rand('Peter','Quagmire','Joe'); echo "$who is $ages[$who] years old"; ?> </body> </html> But after some search I concluded that rand is only used for random numbers So, my question is, which function should I use to achieve my goal? Is there any randstring('string1','string2',etc) command? Tnx for your time currently using function gen_chars($length = 6) { // Available characters $chars = '0123456789abcdefghjkmnoprstvwxyz'; $Code = ''; // Generate code for ($i = 0; $i < $length; ++$i) { $Code .= substr($chars, (((int) mt_rand(0,strlen($chars))) - 1),1); } return $Code; } // Usage $var = gen_chars(12); echo $var; However ide like to know if theres a more efficient way of generating this since this is going to be using a lot. Thanks Hi, I have a script to add images to my tabel and and upload them to my server, I have managed to get the upload script to rename the image with a 5 digit random number on the end to prevent overwriting, the only problem I can't seem to be able to add the same name to my tabel. Any ideas? Thanks in advance. Code: [Select] <?php $idir = "uploads/"; // Path To Images Directory if (isset ($_FILES['fupload'])){ //upload the image to tmp directory $url = $_FILES['fupload']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['fupload']['type'] == "image/jpg" || $_FILES['fupload']['type'] == "image/jpeg" || $_FILES['fupload']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['fupload']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['fupload']['tmp_name'], $idir. basename($_FILES['fupload']['name'], $file_ext).rand(10000 , 99999).$file_ext); // Move Image From Temporary Location To Perm } } if (isset ($_FILES['fupload2'])){ //upload the image to tmp directory $url = $_FILES['fupload2']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['fupload2']['type'] == "image/jpg" || $_FILES['fupload2']['type'] == "image/jpeg" || $_FILES['fupload2']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['fupload2']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['fupload2']['tmp_name'], "$idir" . $_FILES['fupload2']['name'] $file_ext).rand(10000 , 99999).$file_ext); // Move Image From Temporary Location To Permanent Location } } if (isset ($_FILES['fupload3'])){ //upload the image to tmp directory $url = $_FILES['fupload3']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['fupload3']['type'] == "image/jpg" || $_FILES['fupload3']['type'] == "image/jpeg" || $_FILES['fupload3']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['fupload3']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['fupload3']['tmp_name'], "$idir" . $_FILES['fupload3']['name'] $file_ext).rand(10000 , 99999).$file_ext); // Move Image From Temporary Location To Permanent Location } } if (isset ($_FILES['fupload4'])){ //upload the image to tmp directory $url = $_FILES['fupload4']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['fupload4']['type'] == "image/jpg" || $_FILES['fupload4']['type'] == "image/jpeg" || $_FILES['fupload4']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['fupload4']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['fupload4']['tmp_name'], "$idir" . $_FILES['fupload4']['name'] $file_ext).rand(10000 , 99999).$file_ext); // Move Image From Temporary Location To Permanent Location } } $usr = "user"; $pwd = "pass"; $db = "db"; $host = "host"; # connect to database $cid = mysql_connect($host,$usr,$pwd); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } if ($_POST['submit']) { $logo = mysql_real_escape_string("$idir" . $_FILES['fupload']['name']); $image1 = mysql_real_escape_string("$idir" . $_FILES['fupload2']['name']); $image2 = mysql_real_escape_string("$idir" . $_FILES['fupload3']['name']); $image3 = mysql_real_escape_string("$idir" . $_FILES['fupload4']['name']); $SQL = " INSERT INTO mhhire "; $SQL .= " image1, image2, image3, logo) VALUES "; $SQL .= " ('$image1', '$image2', '$image3', '$logo') "; $result = mysql_db_query($db,$SQL,$cid); $last=mysql_insert_id(); if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } header("location:thanks.php"); exit(); } ?> I have seen somewhere that you can add to a string like so $var = "text"; or $string ="var"; &$var = "text"; it is the latter that I am wanting to know more about. This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=314527.0 Hi, I currently have a string with some values in it: $a_random_string="some random text, goes here - $123<br>some more random text goes here - $53 (text 123 )<br>ANother line of text - $126"; Now I want to add all the values that have a '$' before the value eg; $123. So in this case I would end up with: 302 How would I do this? Thanks, mme Is it possible to add a query string for example some_var=jk84 to any sort of link be it, http://www.website.com , http://www.website.com/?some_id=4, or http://www.website.com/post=45&category=9 or http://www.website.com/somepost/ ? While adding that extra query string how can I make sure I'm not affecting the website's content or causing some script error? I have a php line like: echo "<p><b>Tags: </b>" . $row['tags'] . "</p>";
it looks like this on web page: how do i add hyperlink to each keyword like: Tags: <a href='category/keyword1'>keyword1</a>, <a href='category/keyword2'>keyword2</a>, <a href='category/keyword3'>keyword3</a>,...
I have been becoming more and more familiar with PHP over the past few months, but this problem stumps me. How can I take a string of words separated by spaces taken from an XML feed (example: word1 word2 word3 word4) and turn it into something like this: <a href="word1">word1</a> <a href="word2">word2</a> ... and so on. This has to be done on the fly, since it is taking the words from the XML feed, they change depending on the page. I want them to be in a vertical column, and be able to link each one to the search page for that word, so each link has to be different. An even more in-depth problem, how can I sort it then so that there are multiple columns if necessary, say one page only has 3 words, but another has 50. How can I split them up into multiple vertical columns, say with 10 words in each? Have I confused anyone yet? I have a file containing lines (I mean elements separated by <br>). How I can put these lines into an array, but only those which has a given phrase. Example file Code: [Select] This is the first line<br> Second line is here<br> something else<br> something else<br> something more<br> I want to catch only lines which contain the word "something" and make an array. Hi I have an array of items that I am currently displaying a foreach loop What I'm looking to do, is add a string, to all of items, a part from the last 4: foreach ($pager->getResults() as $items => $item) { echo $item->getName(); //need to add a string to this for the all but the last 4 } Thanks Hi all, I am trying to add 3 images to a file on the server. The form is posted to a php page where I am trying to put the images in to a folder named aircraft. I have tried echoing the values and they are correct. But it is not working for some reason. Any help will be appreciated. The html form is: Code: [Select] <li> <label for="image">Main Image</label> <fieldset id="image1"> <input type="file" name="image" id="image" /> </fieldset> <label for="image2">Second Image</label> <fieldset id="image 2"> <input type="file" name="image2" id="image2" /> </fieldset> <label for="image3">Third Image</label> <fieldset id="image 3"> <input type="file" name="image3" id="image3" /> </fieldset> </li> The PHP page is: <?php $destination='aircraft/'.$reg."1.jpg"; $temp_file = $_FILES['image']['tmp_name']; move_uploaded_file($temp_file,$destination); $destination2='aircraft/'.$reg."2.jpg"; $temp_file2 = $_FILES['image2']['tmp_name']; move_uploaded_file($temp_file2,$destination2); $destination3='aircraft/'.$reg."3.jpg"; $temp_file3 = $_FILES['image3']['tmp_name']; move_uploaded_file($temp_file3,$destination3); ?> |