PHP - Get Random Text From Database Table, How To?
Similar TutorialsHi, I'm a researcher (and complete coding noob), and am planning a longitudinal study that requires e-mail follow-up with subjects taking an initial survey. For purposes of ethics/anonymity due to sensitive survey data, I'd like the acquired e-mails to be saved uncoupled from the survey responses; this is simple to deal with, and I use a basic PHP e-mail form, which injects the email address in a table in MySQL in a different server than the one used for the survey. The issue is that the e-mails are saved in the MySQL database in order of injection, thus it is still theoretically possible for me to link the e-mails back to the survey responses (which have a time stamp that I cannot remove). Ideally I would like not to be able (at all) to link the e-mails to the survey responses, and one way to do that (since I don't save the e-mail injection timestamps in MySQL) might be to have the e-mails saved in MySQL in a random order. Not sure if this is possible, and not even sure if this would be via PHP or MySQL side of things. The server is on godaddy and uses Starfield interface for MySQL but I cannot find an option for random insert/saving of table items (emails). They are saved in order of injection. Any solution for this? Thanks, the second database found on the cloud
i try to get JSON data but how to insert and update them to another online database with the same table my php script to return json data <?php include_once('db.php'); $users = array(); $users_data = $db -> prepare('SELECT id, username FROM users'); $users_data -> execute(); while($fetched = $users_data->fetch()) { $users[$fetchedt['id']] = array ( 'id' => $fetched['id'], 'username' => $fetched['name'] ); } echo json_encode($leaders);
i get
{"1":{"id":1,"username":"jeremia"},"2":{"id":2,"username":"Ernest"}} Edited March 24 by mahenda Hello, I need some help. Say that I have a list in my MySQL database that contains elements "A", "S", "C", "D" etc... Now, I want to generate an html table where these elements should be distributed in a random and unique way while leaving some entries of the table empty, see the picture below. But, I have no clue how to do this... Any hints? Thanks in advance, Vero Hi, Would someone please give me some assistance on how to display random products from my database I need to display a maximum of 6 images and there link in two rows of 3 so; [image] [image] [image] [image] [image] [image] and each image would have something like; <a href="<? echo $row['product_link'] ?>"><img src="<? echo $row['product_image'] ?>"/></a> but i don't know how to display these at random and make sure only 6 appear at a time. Thanks for any help, hope you understand what im trying to explain Code: [Select] require('../conn/db_connection.php'); //seleksi restoran $pilih_resto="select * from restoran"; $query_pilih_resto=mysql_query($pilih_resto); echo "<h3>Truly Data : </h3>"; while($resto=mysql_fetch_array($query_pilih_resto)){ echo"<font color='red'>".$resto["id_restoran"].".". $resto["nama_restoran"]."</font><br>"; $seleksi_menu_1="select * from jenis_menu"; $query_menu_1=mysql_query($seleksi_menu_1); while($menu_1=mysql_fetch_array($query_menu_1)){ echo "<font color='green'>".$menu_1["id_jenis_menu"]."</font><br>"; $seleksi_data_eat="select * from eat where id_restoran like '".$resto["id_restoran"]."' and id_jenis_menu like '".$menu_1["id_jenis_menu"]."'"; $query_eat=mysql_query($seleksi_data_eat); while($eat=mysql_fetch_array($query_eat)){ echo $eat["nama_eat"]."<br>"; } } echo"<br>"; } echo "------------------------------<br>"; echo "<h3>Random Data : </h3>"; $pilih_resto_2="select * from restoran"; $query_pilih_resto_2=mysql_query($pilih_resto_2); $menu_PNP="PNP"; $menu_PBK="PBK"; $menu_UTM_1="UTM"; while($data_resto=mysql_fetch_array($query_pilih_resto_2)){ echo "<b>".$data_resto["id_restoran"]."-".$data_resto["nama_restoran"]."</b><br>"; $seleksi_data_eat_PNP="select * from eat where id_restoran like '".$data_resto["id_restoran"]."' and id_jenis_menu like '".$menu_PNP."'"; $seleksi_data_eat_PBK="select * from eat where id_restoran like '".$data_resto["id_restoran"]."' and id_jenis_menu like '".$menu_PBK."'"; $seleksi_data_eat_UTM="select * from eat where id_restoran like '".$data_resto["id_restoran"]."' and id_jenis_menu like '".$menu_UTM_1."'"; $query_eat_UTM=mysql_query($seleksi_data_eat_UTM); $jml_data_UTM=mysql_num_rows($query_eat_UTM); echo "Summary of main Menu : $jml_data_UTM<br>"; $query_eat_PNP=mysql_query($seleksi_data_eat_PNP); $query_eat_PBK=mysql_query($seleksi_data_eat_PBK); while($eat_PNP=mysql_fetch_array($query_eat_PNP)){ $data_eat_PNP[]=$eat_PNP["nama_eat"]; } while(list($index_PNP,$data_PNP)=each($data_eat_PNP)){ $data_eat_PNP_list[$index_PNP]=$data_PNP; } while($eat_PBK=mysql_fetch_array($query_eat_PBK)){ $data_eat_PBK[]=$eat_PBK["nama_eat"]; } while(list($index_PBK,$data_PBK)=each($data_eat_PBK)){ $data_eat_PBK_list[$index_PBK]=$data_PBK; } while($eat_UTM=mysql_fetch_array($query_eat_UTM)){ $eat_UTMx[]=$eat_UTM["nama_eat"]; } for($i=0;$i<$jml_data_UTM;$i++){ $random_data_PNP[$i]=array_rand($data_eat_PNP_list,1); $random_data_PBK[$i]=array_rand($data_eat_PBK_list,1); echo "<font color='red'>".$data_eat_PNP_list[$random_data_PNP[$i]]."</font>---<font color='green'>". $eat_UTMx[$i]."</font>---<font color='blue'>".$data_eat_PBK_list[$random_data_PBK[$i]]."</font><br>"; } } Please Help, There is a random error data how to create random data like this? Data => Restoran A: Menu appetizers : - Sup - Sate Usus - Trancam Main Menu - Nasi Goreng - Nasi Goreng Putih - Nasi + Ayam Penyet - Nasi + Bebek Panggang - Ikan Gurame Asam + Nasi - Nasi Soto Menu Dessert - Pudding - Buah Segar - Juice Aplukat Restoran B : Menu appetizers -Sup Ikan Paprika -Sup Jamur Spesial -Sup Urat Sapi Saus Tiram -Sup May Jala - Jala Main Menu - Bubur Ayam Kombinasi - Bubur Ayam/Sapi Clay Pot - Bubur Telor Pitan Clay Pot - Gurame Panggang - Pangsit Udang - Cumi-cumi Goreng Special - Kepiting Lembek Mentega - Sayap Ayam Panggang Dessert -Juice Alpukat -Mix Juice -Sui Kiau Goreng Salad -Ha Kaou Sayuran Out Put : Restoran A : Menu e ---+--- Main Menu --+---- Dessert ---------------------------------------------------- Sup ------- Nasi Goreng ------- Pudding Sup ------- Nasi Goreng Putih ------- Juice Aplukat Sate Usus ------- Nasi + Ayam Penyet ------- Pudding Sup ------- Nasi + Bebek Panggang ------- Buah Segar Trancam ------- Ikan Gurame Asam + Nasi ------- Buah Segar Sate Usus ------- Nasi Soto ------- Pudding Restoran B : Menu Pembuka ---+-- Menu Utama --+--- Menu Penutup ----------------------------------------------------------------------- Sup Ikan Paprika ------- Bubur Ayam Kombinasi ------- Juice Alpukat Sup Urat Sapi Saus Tiram ------- Bubur Ayam/Sapi Clay Pot ------- Sui Kiau Goreng Salad Sup May Jala-Jala ------- Bubur Telor Pitan Clay Pot ------- Juice Alpukat Kepiting Lembek Mentega ------- Gurame Panggang ------- Juice Alpukat Sup Urat Sapi Saus Tiram ------- Pangsit Udang ------- Mix Juice Sup May Jala-Jala ------- Cumi-cumi Goreng Special ------- Ha Kaou Sayuran Sup Jamur Spesial ------- Kepiting Lembek Mentega ------- Sui Kiau Goreng Salad Sup May Jala-Jala ------- Sayap Ayam Panggang ------- Ha Kaou Sayuran Note : appetizers and desserts at random based on count of data main menu hi. i am making mcq type test. lot of questions stored in db, i want that some of them retrieve i.e. 5 after 5 test should end. how is hould do, query is ...SELECT question,questionid, choice1, choice2, choice3, choice4, answer FROM questionbank order by RAND() limit 1";... limit 1 is used for display one question in a page. I'm trying to generate a unique "dosieid" number for my web site.My web site is a human resources program solution, in that program users create dosie of their workers in their firm ...random dosieid needs me so when user createing dosie in field dosieid automaticly show the dosieid-s that are not used before...the dosieid that don't exist in database. In other case I would use auto increment but in this case dosie is not created yet. And in form dosieid must be option to change the number if random is not fine with a user. One more hint the numbers must bee from 1 to 9999. Can someone help mee? I have try many codes but i have not find something like one with this spec. This is what I have do so far. It gets the random number but I don't know how to compare that random number with database row "dosieid" ? Code: [Select] <? $id_num = mt_rand(1,9999); $query = "SELECT dosjeid FROM albums"; $result = mysql_query($query) or die(mysql_error()); while($account = mysql_fetch_array($result)){ if ($id_num == $account['id']){ $id_num = mt_rand(1,9999); } } echo"$id_num<br>"; ?> Hello, I am using the following code to display images managed by a MySQL database. Basically another program manages a bunch of images, but this script displays certain ones (ones with INCLUDE = 1 in the database) on my main page. My question is, is there an easy way to limit the number of images it displays, say to 5? I'm not too concerned which images actually display (ascending or descending)... or better yet, random! Most importantly, I only want five to display. Each image will be linked to the full page, which displays all the images. Any ideas? Thanks! Code: [Select] <?php $username="XXXXXXX"; $password="XXXXXXX"; $database="XXXXXXX"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM ft_form_12 WHERE col_24='1'"; // $query="SELECT * FROM ft_form_12"; // SELECT * FROM ft_form_12 WHERE col_24='1' $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <?php $i=0; while ($i < $num) { $f20=mysql_result($result,$i,"col_23"); //Photo file name $f21=mysql_result($result,$i,"col_24"); //INCLUDE ?> <a href="http://www.domain.com/display_whole_page.shtml"><img src="http://www.domain.com/the_file/pictures/<?php echo $f20; ?>" height="50" border="0"></a> <?php $i++; } ?> hi!, is it possible to use rand() to generate unique number that will be saved in a database as a primary key? Since i dont want to have numbers like 00001 incrementing on the table. They dont look like real account numbers... i need something like 45642 or 95452 and the like. thanks in advance. I need a way to generate a random number and insert into a database, and I need the database to not contain any duplicates of that number. I basically need to generate a RMA # (Return Merchandise Authorization Number), so the numbers absolutely CAN NOT be duplicate. I have no ideas how to go about this. Should I generate a random number, search the database and see if there's a duplicate, and if there is re-run the script? Ok say i have a database with users who have a video url field for them to enter a video url to be submitted to the database, im trying to create a page where the videos will be displayed one at a time for example ' 1st video plays then it switches to the next random video from the database and so on' i want only to play so many seconds of the video before it switches as they will be presentations so i dont want viewers to watch them all in full, could anyone help me out with some code or feedback on this please? Hi, What I'm trying to do is create a script that generates a random list of cars whenever it is run. I have a list of available cars in a MySQL database. I'm just not sure how to get php to generate a list of, say, 20 cars, that are randomly picked from the cars in the database. I may also want to add other statistics like MPG (also already in the database). Can anyone show me how to do this? And is it possible to format the returned data in a table or div format? Thanks in advance Unk Hi
My knowledge about this stuff is less than basic, I am using this code which I found, changed it little bit with help of logic as original one was not working and make it work but.... I would like to place frames around avatars while avatars are in horizontal order one next to each other...and under them there is nick names
So all this is done on Vbulletin board, within one custom widget
This is my code now, what I need is frames and that nick names are centered :
$member_count = 6; ob_start(); require_once('./includes/functions_user.php'); require_once('./includes/functions_bigthree.php'); // Get Random Members $newusers_get = vB::$db->query_read(" SELECT ".TABLE_PREFIX."user.userid AS userid, ".TABLE_PREFIX."user.username AS username, ".TABLE_PREFIX."user.avatarrevision AS avatarrevision, ".TABLE_PREFIX."customavatar.dateline AS dateline FROM ".TABLE_PREFIX."customavatar LEFT JOIN ".TABLE_PREFIX."user ON ".TABLE_PREFIX."customavatar.userid=".TABLE_PREFIX."user.userid WHERE ".TABLE_PREFIX."customavatar.visible = 1 ORDER BY RAND() LIMIT $member_count"); $output_bits = '<table cellpadding="5" align="center"><tr>'; while($newuser = vB::$db->fetch_array($newusers_get)) { $output_bits .= '<td><a href="member.php?u='.$newuser[userid].'"><img src="image.php?u='.$newuser[userid].'&dateline='.$newuser[dateline].'" alt="'.$newuser[username].'"/ width="120" height="120"><br />'.$newuser[username].'</a></td>'; } $output_bits .= '</tr></table>'; $output = $output_bits; ob_end_clean();And in attached photo you can see how it looks At least point me in a right direction, not sure what tor read or where to look Thank you Goran Attached Files Untitled 0111.jpg 29.24KB 0 downloads Not sure where to put this, so I figured this would be a good place to start.
First:
I host a website for a client through iPage.com called Authentic Log Houses at authenticloghouses.com.
This website uses a wordpress platform.
Anyway, on with the issue. My client called me one day and said a text ad showed up on his navigation menu of the website selling viagra! Immediately the worst case scenario went through my mind. I told him I'd check it out as soon as I could. I got home and checked his website using Chrome (my preferred browser) only to find it exactly as it should be (with no ad). I assumed at that point that he was probably the only one that could see the link and was probably just dealing with a virus or malware that was affecting his browser.
I told him he should run an antivirus or malware removal program and see if he still saw the ad. He did, but found nothing. He then called his brother and asked him to view his website to see if the ad appeared. It did. I checked again on my computer, still nothing. Finally I decided to try a different browser than Chrome and found that Firefox, IE, Safari, and Opera ALL show the ad!
I still don't know why every browser except Chrome shows the ad, but now that I could duplicate the issue, I eliminated the possibility of a virus or malware since multiple computers showed the same ad (which changes from time to time). I viewed the source code to see where it may have been coming from and it simply showed an anchor tag with a link to a random website in the main nav menu outside the un-ordered list that the website uses, however, I cannot find that link in the source files.
I spoke with iPage's technical support to see if they had any ideas and they said to update all my plugins and install the "Link Removal Tool" and enter the linked url. They said this tool has helped many people and works really well. Nothing worked for me. I even tried their "Pro" version (which is still free o_O) and it seemed to be nothing but spam, so now I don't trust them in the least.
The current text ad can be view on the website in green in the top navigation bar saying "Payday loans direct lender no teletrack" and links to a 404 at worldwiderssfeedcompendium.com.
I'm currently out of ideas! Any help would be awesome. I've never seen this before! I can upload any files if necessary.
Thanks in advance,
Kevin
Hi, I am wanting to create a random fact script for a small website project. The idea is when the user loads a page a random fact will appear at the top ( I guess from an array of strings I will make?) Can anyone recommend the most efficient and easiest way to go about this please? How would I compare 2 random numbers to rows in a table. The table has 4 rows and 2 colums. My table is setup like this: ID hp mp then has just randomly placed numbers from 0 to 5. So far my script looks like this: I have $new_hp=rand(0,5); $new_mp=rand(0,5). Then i query the db by $myquery=doquery("select hp,mp from users"; $myrow=mysql_fetch_array($myquery); once this is done would a while statement like while ($myrow = mysql_fetch_array($myquery) to what each line in the table has? I would then need to compare the random numbers to those entries in the database. Something like if($myrow[hp] == $new_hp && $myrow[mp] == $new_mp) { ? But if the if statement isnt true it needs go back choose another random number and run same again and again until the statement is true and that is where im stuck at so any help would be greatly appreciated I have a user table that holds email addresses and for testing purposes I am trying to replace every email address with an email address defined in an array. I would like to randomly choose an email address from the array and update the table with this address. When I run the following code it randomly chooses a email address from the array but then updates every row with this one email address. Can you someone please let me know what I am doing wrong. Thanks in advance. Code: [Select] $query = "SELECT * FROM user '"; $result = mysql_query($query); $input = array('email1', 'email2', 'email3', 'email4', 'email5', 'email6', 'email7'); $rand_keys = array_rand($input, 2); $replaceStr = $input[$rand_keys[0]]; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $rand_keys = ""; $rand_keys = array_rand($input, 2); $replaceStr = $input[$rand_keys[0]]; mysql_query("UPDATE user SET email = '$replaceStr'"); } Hi Guys, I wonder if you can help me before I go crazy? I have the task of updating a site by adding text to every page, at the moment there are three pages on the live website that show both images and text, I am to update the remaining 6 with text to go with the images.... I thought this was all fine and dandy, added the text in, checked the code, uploaded to the FTP site and nothing, only 1 page worked. I am stuck as I have used the same method to add text to all the pages yet those elusive pages refuse to show text! This is driving me crazy and i am hoping that one of you guys has come across this before and will be able to highlight a missing piece of code or have a solution? I do appreciate your time and any suggestions that you have. I have attached the page below. Thanks guys! This is a section of my code that goes and gets videos from any video site. What I’m having issues with is having it post a random title, that are all in a text file called titles.txt. What I’m wanting to accomplish is have my script go pull the video embed code, the duration, the tags, etc. Which all works fine until I get to the title part where it pulls locally from the server from the text file. I'm not an advance programmer so not sure if I am even approaching this correct way. Here is the code I have that is responsible for putting the title in. I appreciate your help. $video = array( ‘user_id’ => $this->user_id, ‘status’ => $this->status, ‘site’ => ‘my site’, ‘id’ => ‘’, ‘embeddable’ => true, ‘url’ => ‘’, ‘titles’ => ‘’, ‘title’ => ‘’, ‘description’ => ‘’, ‘tags’ => ‘’, ‘category’ => ‘’, ‘thumbs’ => array(), ‘duration’ => 0, ‘embed’ => ‘’ ); function random_title () { $titles = file (“titles.txt”, FILE_IGNORE_NEW_LINES); $num = rand (0, intval (count ($titles) / 3)) * 3; return ucwords($titles[$num]); } //Title if(preg_match(’/title="(.*?)"/’, $match, $matches_title)) { $video[‘title’] = random_title(). ’ - My site name’; } else { $this->errors[] = ‘Failed to get video title for ‘.$video[‘url’].’!’; if (!$this->debug) continue; else $debug_e[] = ‘TITLE’; }
|