473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
13 1941
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
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
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
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
thanks guys, i am really learning something tonight.

piece by piece I'm getting there.

Sep 10 '05 #6

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

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
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
Print would also work, Print_r prints the value

Sep 11 '05 #12
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class,...
33
by: John Timbers | last post by:
I'd like to purchase Visual C# .Net for learning purposes only since it's a lot cheaper than Visual Studio (note that I'm a very experienced C++ developer). Can someone simply clarify the basic...
6
by: Rated R1 | last post by:
I have a form that is calculating a labor cost based on some combo boxes. Now there are 75 different styles that I am basing it on and I have it working no problem so that I dont need any help...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
0
by: Alex | last post by:
Hi, an LPCTSTR is a long pointer to a const string. This is a windows typicall variable type like UINT, DWORD and so on. You can use it the same way like you would use standard variable types...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
6
by: **Developer** | last post by:
usually I'd do: Drawing.Image.FromFile( I noticed I once did without thinking: Drawing.Bitmap.FromFile( I assumed this worked because Bitmap inherits from Image, but for fun I thought I'd...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
13
by: Eric_Dexter | last post by:
All I am after realy is to change this reline = re.line.split('instr', '/d$') into something that grabs any line with instr in it take all the numbers and then grab any comment that may or may...
37
by: Prafulla T | last post by:
Assume that an integer pointer x is declared in a "C" program as int *x; Further assume that the location of the pointer is 1000 and it points to an address 2000 where value 500 is stored in 4...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.