Page 1 of 1

(SOLVED) Crazy learning PHP for a UDT. Can you help...

Posted: Wed Jul 13, 2011 4:25 am
by Andrew Prior
I have around 100 images that need to display randomly in a couple of locations across a CMSMadeSimple site (1.9)

All will have title tags, some will contain urls as well.

I constructed a two column table called cms_pics, with the columns: id, titletag.
Id is simply an auto increment number, title tag is the code for the image eg

Code: Select all

<img src="uploads/images/{$num}.jpg" alt="Australia" title="Australia" />
{$num} is a randomly generated mumber corresponding to a row in the table

I tried to follow online tutorials but am totally lost. My UDT will accept the code below as valid, but displays nothing.

What I am trying to arrive at is an output string something like this:

Code: Select all

<img src="uploads/images/25.jpg" alt="Bicycle on Road" title="Riding the Great Ocean Road" />
This is my non working code. What do I need to fix?

Code: Select all

mysql_connect("localhost","user","password");
mysql_select_db("mydatabase");  
$result = mysql_query("SELECT * FROM cms_pics WHERE id = '1' ");  //id = $num
while($rows = mysql_fetch_assoc($result)){
echo $rows[$titletag]; 

}
You can tell me I know nothing... because it is true!
Any help would be great

Re: Going Crazy learning PHP for a UDT. Can you help, please

Posted: Wed Jul 13, 2011 4:59 am
by Duketown
Andrew,

Maybe you can use:

Code: Select all

// Make a connection to the database
$db = cmsms()->db;
$query = 'SELECT * FROM '.cms_db_prefix().'pics';
$dbresult = $db->Execute($query );

// Now show the images information
while ($dbresult && $row = $dbresult->FetchRow())
{
  $num = $row['num'];
  $alt = $row['alt'];
  $title = $row['title'];
  echo '<img src="uploads/images/'.$num.'.jpg" alt="'.$alt.' title="'.$title.'" />';
  echo "\n";
}
Hope this helps,

Duketown

Re: Going Crazy learning PHP for a UDT. Can you help, please

Posted: Wed Jul 13, 2011 10:44 am
by Andrew Prior
Well, mate
if you are ever in Adelaide, Google me.
I owe you a beer.
That code is nothing like what I was imagining! :) But it shows me the images, which is great.

I am actually wanting to show only one image when the page loads, based on a random generated number, so I need some kind of WHERE statement. I will try and work this out, but you may be able to do it in 3 or 4 keystrokes, which I would gratefully accept!
Andrew

Re: Going Crazy learning PHP for a UDT. Can you help, please

Posted: Wed Jul 13, 2011 12:05 pm
by Andrew Prior
Dketown, adjusting your code below (line 4) gives me just one image by choosing the image where num=2.

Code: Select all

// Make a connection to the database

$db = cmsms()->db;
$query = 'SELECT * FROM '.cms_db_prefix().'pics WHERE num=2';
$dbresult = $db->Execute($query );

// Now show the images information
while ($dbresult && $row = $dbresult->FetchRow())
{
 $num = $row['num'];
  $alt = $row['alt'];
  $title = $row['title'];
  
echo '<img src="uploads/images/headers/header_'.$num.'.jpg" alt="'.$alt.'" title="'.$title.'" />'; 
  echo "\n";
}
However i cannot seem to define a random number which is then parsed by that line. If I make a variable which equals 2, for example, this will not work.

Code: Select all

$fred="2";
$query = 'SELECT * FROM '.cms_db_prefix().'pics WHERE fred=2';
Can you tell me the correct syntax?

Re: Going Crazy learning PHP for a UDT. Can you help, please

Posted: Wed Jul 13, 2011 2:11 pm
by Jos
How about this:

Code: Select all

$query = "SELECT * FROM ".cms_db_prefix()."pics ORDER BY RAND() LIMIT 0,1";

Re: (SOLVED) Crazy learning PHP for a UDT. Can you help...

Posted: Thu Jul 14, 2011 5:15 am
by Andrew Prior
Thankyou Jos and Duketown. Works Great. It's at onemansweb.org The code is in the pictures on the left bottom of the page. Andrew