Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 09-10-2005, 06:22 PM
kjhkjh's Avatar
kjhkjh kjhkjh is offline
 
Join Date: Jul 2005
Posts: 77
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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?
Reply With Quote
  #2  
Old 09-11-2005, 05:15 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

What vB version are you using?
And is this a standalone script, or do you use it inside your forum somehow?
Reply With Quote
  #3  
Old 09-11-2005, 10:29 AM
kjhkjh's Avatar
kjhkjh kjhkjh is offline
 
Join Date: Jul 2005
Posts: 77
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #4  
Old 09-11-2005, 10:34 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Instead of print_r use:
PHP Code:
echo "$randgetresults[token]"
Reply With Quote
  #5  
Old 09-11-2005, 10:49 AM
kjhkjh's Avatar
kjhkjh kjhkjh is offline
 
Join Date: Jul 2005
Posts: 77
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:03 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.08040 seconds
  • Memory Usage 2,197KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (5)post_thanks_box
  • (5)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (5)post_thanks_postbit_info
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete