By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,491 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,491 IT Pros & Developers. It's quick & easy.

php question. Pls take a look and see if you can help

P: n/a
Hi All,

I've created a new table (eg/ 'XM_newtable') on my 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
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

Sep 10 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
I use a random number to do things like this. Put in a sort field and
put a random number from 1 to 100 in it. Parse the records and sort
according to that number.

Sep 10 '05 #2

P: n/a
Wish I had googled before I wrote mine. Nah, it helped me.

If you are in a hurry however, take a look at this.

http://scripts.franciscocharrua.com/...result-set.php

Sep 10 '05 #3

P: n/a
http://www.freewebmasterhelp.com/tutorials/phpmysql

This URL can show you how to access records, print them out, loop
through, and display an error if none exist.

I would just have an ID field that's auto-increment from 1-100 then
generate a randon number and print out the results from the field with
the ID corresponding to the randomly generated number.

Sep 10 '05 #4

P: n/a
thanks for the quick reply. I'm still a little confused.

i am such a newb to this. the link seems to include a lot more that's
needed. I'm going to have another read through the script again to see
if I can use any of it.

One thing is that the number of entries in the fields will be
constantly changing as other php pages are adding entries and taking
some away and so I'm not to sure how to do the random bit if the total
number of entries are always changing.

Thanks for your help

Sep 10 '05 #5

P: n/a
thanks guys, i am really learning something tonight.

piece by piece I'm getting there.

Sep 10 '05 #6

P: n/a

<ne*********@mail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
thanks for the quick reply. I'm still a little confused.

i am such a newb to this. the link seems to include a lot more that's
needed. I'm going to have another read through the script again to see
if I can use any of it.

One thing is that the number of entries in the fields will be
constantly changing as other php pages are adding entries and taking
some away and so I'm not to sure how to do the random bit if the total
number of entries are always changing.


do a count first so that the script know how many numbers /entries there are
and the selects a number in that range.
Sep 10 '05 #7

P: n/a
Do your program logic on paper or flow chart before you code. Will save
yourself some time, or at least it does me. All about planning.

Sep 10 '05 #8

P: n/a
Thanks for your help so far, I think that I'm getting there and have
come up with what follows which isn't working but maybe you can spot
what's wrong with it.

The basics is that I have a table on the database that has a single
field (i have been trying some other stuff which includes an ID number
field but I wondered if anyone can help me fix the stuff below so I
don't need it).

The single field has about 100 entries so far but this will always
change and at some times there may be no entries (see the if / else
clause).

Otherwise I need to display a random entry from the table.

Here goes...

<?php
$username="dbusername";
$password="dbpassword";
$database="dbname";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$chooseall="SELECT * FROM tablename";
$chooseallresults=mysql_query($chooseall);
$numrows=mysql_numrows($chooseallresults);

$randget="SELECT * FROM tablename ORDER BY RAND() LIMIT 1";
$randgetresults=mysql_query($randget);

if ($numrows=0) {echo "Sorry no entries"
} else {
echo "$randgetresults"
mysql_close();
?>

Sep 11 '05 #9

P: n/a

This pretty much does what I need it to but it adds some code in the
result. (I mean instead of just showing the "random entry", it shows
"Array ( [field] => random entry )". It would be great if I could get
rid of all the other stuff. Here's my code so far. Can anyone tidy up
my result to get it to do what I want it to?

Thanks

<?php
$username="username";
$password="password";
$database="database";

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();
?>

Sep 11 '05 #10

P: n/a
Someone just helped me with the answer so I thought that I'd let
everyone else know (incase anyone else in future wants to know how to
do this)
The Answer:

Replace "print_r($randgetresults);"

with "echo "$randgetresults[token]";"

Thanks for all your help.

Sep 11 '05 #11

P: n/a
Print would also work, Print_r prints the value

Sep 11 '05 #12

P: n/a
On 2005-09-10 21:36:48 +0000, "Darkstar 3D" <st********@gmail.com> said:
Do your program logic on paper or flow chart before you code. Will save
yourself some time, or at least it does me. All about planning.


You mean dead trees? How about OmniGrafle?

--*
Seks, seksić, seksolatki...<uri: news:pl.soc.seks.moderowana > <~|{ A.A }|
Love, give me a reason to be beautiful! Miles and miles of perfect skin,
*I said I fit so perfect in! I'm fading like a rose...Give me a reason!!!
https://hyperreal.info | https://kanaba.info |=> "Go¶ciu! Szanuj Zieleń!"

Oct 1 '05 #13

P: n/a
Paper works best for some, programs work for others. Programs work
great for me, mainly Visio, but lately I have been using FreeMind.

Oct 1 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.