The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
||||
|
||||
PHP question. Please take a look and see if you can help
Hi All,
I've created a new table (eg/ 'LS_newtable') on my vB database and in it I have a single field (eg/ 'newfield') I have entered 100 values under the 'newfield' and what I need to do is display these values at random wherever I insert a bit of code. My problem is that I don't know what bit of code to insert :disappointed: I guess that it will be a code similar to below. But what would the direct path to this field be? And how can I make the result be random from this field? <?php print("$newfield"); > The final thing that I would like is that if the field doesn't have any entries (so if I take out the 100 values that I've put in) then I would like to have the code print the message "sorry but no value available!" All help is always appreciated. Thanks i've just added an incremental id number for another column in the table. I think that there must be a way to use this to help create the random bit and then provide the corresponding value from row 2. any help? |
#2
|
|||
|
|||
What vB version are you using?
And is this a standalone script, or do you use it inside your forum somehow? |
#3
|
||||
|
||||
KirbyDE has really helped me out and we got to this point:
<?php $username="dbusername"; $password="dbpassword"; $database="dbname"; mysql_connect('localhost',$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $randgetq=mysql_query("SELECT * FROM tablename ORDER BY RAND() LIMIT 1"); if (!$randgetresults=mysql_fetch_array($randgetq, MYSQL_ASSOC)) { echo "Sorry no entries"; } else { print_r($randgetresults); } mysql_close(); ?> It is almost perfectly The small thing is that on the website when it prints the random entry from the table, it is also prints part of the code in the print output - ie/ it prints "array ( [tablefield] => 5456 )" when I just want it to print "5456" or any other random entry from the table) Do you know why this might be or how I can stop it and just have the field entry printed? My vB version is 3.0.8 |
#4
|
|||
|
|||
Instead of print_r use:
PHP Code:
|
#5
|
||||
|
||||
Thankyou, thankyou, thankyou.
You have added the last piece to this puzzle which has taken me 4 days so far! I really appreciate it. One other question. So this random value [token] is there an easy way that I can put it wherever I want on my vB site. So perhaps If I want to display this to people on the homepage in the corner (or on a thankyou page after they post a message) I could add something smaller than the whole code. If I could stick <?php $result ?> Into the html of the page and have it show the result of the full code then that would be awesome. I guess something would be needed in the "php includes" --------- NEWS: I found the answer and other newbs may find it useful so here it is: In the phpinclude_start template through the admin cp, include the following: ob_start(); include_once('path/to/yourphppage.php'); $variable = ob_get_contents(); ob_end_clean(); Now you can add as many of these groups of code into the phpincludes_start template just change the yourpage.php and $variable names. Then just upload your php page. And then insert $variable into your other templates which publish the html and $variable is converted into the result of the .php file. Very clever stuff and it means you can just add $variable wherever you want the result to appear. It feels good to finally start to understand all of this |
Thread Tools | |
Display Modes | |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|