vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   PHP question. Please take a look and see if you can help (https://vborg.vbsupport.ru/showthread.php?t=96003)

kjhkjh 09-10-2005 06:22 PM

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?

Marco van Herwaarden 09-11-2005 05:15 AM

What vB version are you using?
And is this a standalone script, or do you use it inside your forum somehow?

kjhkjh 09-11-2005 10:29 AM

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

Marco van Herwaarden 09-11-2005 10:34 AM

Instead of print_r use:
PHP Code:

echo "$randgetresults[token]"


kjhkjh 09-11-2005 10:49 AM

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 :)


All times are GMT. The time now is 04:42 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.00982 seconds
  • Memory Usage 1,721KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete