Jump to content

[SOLVED] Need The Code?


Hoddz

Recommended Posts

Heya guys, I'm not really new to PHP, but I have lost all of my old files so I am looking for a genius to help me with a bit of code. I'm not sure if it's even possible at the moment, but I may as well give it a shot.

 

The Idea

I would like to display an image that is related to the current day, so I have a database that has the HTML in it to display and link the image, and each has an "ID" on it, which is the day (Sunday, Monday ... Friday, Saturday) and I would like to display one from the current day at random.

 

The Database

Well the database has the current fields in it...

id - with the day in it

code - this has the html code to be displayed when it is picked

 

What I Have So Far

At the moment, I have this code that works out the current day, I am using the $studioday" as it is offset from the server time.

$serverday = date("l",time()); 
$studioday = date("l",time() + (5 * 60 * 60 ) + (2 * 60));

 

What I'm Looking For

What I need is a way to display the HTML for the current day, only one of them if there are more than one for that day, at random. Maybe with some variables for the database name, user, password and all that connection stuff.

 

 

Thanks in advance PHP Freaks!!

Link to comment
Share on other sites

It might be a bit easier to do if you have an idnumber column on your database. An autoincrimenting number, and set it as the primary key.

 

If you made that change, this is the logic for it (too much code for a "help" post):

 

Query using todays day, getting the ids that match, and putting them in to an array.

 

Get the size of the array with count, then send that to rand as the max, 0 as the min.

 

Another query with the id that had an array key that came from rand, getting the HTML and echoing it.

 

 

Hope this makes sense. There's probably a more efficent way to do it (like in MySQL fully), but that's just what comes to mind.

Link to comment
Share on other sites

Okay, but is there not a way to display it using a query? It was what I planed on doing but as I have lost all previous files and I can't find the tutorial for running a query on the net, I thought you guys could help out.

Just an idea.

Link to comment
Share on other sites

so you'd want a query like this ( I think ):

<?php
$query = "SELECT code FROM imgtable WHERE id = '$studioday' ";
$result = mysql_query($query) or die (mysql_error()."<br />Couldn't execute query: $query");
if ($row = mysql_fetch_array($result)) {
  echo '<img src=" ' . $row['code'] . ' " alt=" ' . $studioday . ' " />';
}
?>

 

This would find the current day in the database (based on your variable $studioday).  Then display the contents of code. In this case, I assumed the "code" would be the file path to the image (example.....images/Friday.jpg)

Link to comment
Share on other sites

you mean to connect to the database?

 

Something like this:

<?php
/*  Database Information - Required!!  */
/* -- Configure the Variables Below --*/
$dbhost = 'localhost'; // host name
$dbusername = 'username'; // database username
$dbpasswd = 'password'; // database password
$database_name = 'databasename'; //database name


/* Database Stuff, do not modify below this line */

$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd")
or die ("Couldn't connect to server.");

$db = mysql_select_db("$database_name", $connection)
or die("Couldn't select database.");

?>

Link to comment
Share on other sites

you have to extract the file binary from the image, then place it in the database it is about 10 words to do this, then u make a dud file with the image extraction SQL and then u use the dud as the image by passing it the id in the query string throgh the url. <img src="img.php?image=blah"> this is ofcoure highly supceptable to XSS

Link to comment
Share on other sites

This is what I have...

<?php
// Get the current Studio Day
$serverday = date("l",time()); 
$studioday = date("l",time() + (5 * 60 * 60 ) + (2 * 60));

// Connect to the database (values taken out for security)
$dbhost = 'localhost'; // host name
$dbusername = 'username'; // database username
$dbpasswd = 'table'; // database password
$database_name = 'name'; //database name

$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd")
or die ("Couldn't connect to server.");
$db = mysql_select_db("$database_name", $connection)
or die("Couldn't select database.");

// Show the random image
$query = "SELECT * FROM presents WHERE id = '$studioday' LIMIT = '1' ";
$result = mysql_query($query) or die (mysql_error()."<br />Couldn't execute query: $query");
if ($row = mysql_fetch_array($result)) {
  echo ' . $row['code'] . ';
}

?>

Link to comment
Share on other sites

looks like homework to me ? or did they hire you and you told them you had PHP skill ?

 

LOL! It's for a site I am building actually. I am an Apprentice Carpenter for a day-job if you must know, not that it matters.

Link to comment
Share on other sites

I changed the code, and now I have this...

<?php
// Connect to the database
$dbhost = 'localhost'; // host name
$dbusername = 'username'; // database username
$dbpasswd = 'password'; // database password
$database_name = 'database'; //database name

$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd")
or die ("Couldn't connect to server.");
$db = mysql_select_db("$database_name", $connection)
or die("Couldn't select database.");

// Get the current Studio Day
$serverday = date("l",time()); 
$studioday = date("l",time() + (5 * 60 * 60 ) + (2 * 60));

// Show the random image
$query = "SELECT * FROM presents WHERE id = '$studioday' LIMIT = '1' ";
$result = mysql_query($query) or die (mysql_error()."<br />Couldn't execute query: $query");
if ($row = mysql_fetch_array($result)) {
  echo $row['code'];
}
?>

 

But I'm getting the error...

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '= '1'' at line 1
Couldn't execute query: SELECT * FROM presents WHERE id = 'Friday' LIMIT = '1'

Link to comment
Share on other sites

good good, well, there are two ways to do this, if u have a couple of images only it is better to store the path or the image name to the db like i think u have.

 

You need to generate a random number and base some sort of randomisation on that number, HOWEVER U FEEL be creative put together a little mechanism

Link to comment
Share on other sites

Well the idea of it is to display a random code that is from the database based on the current day. So there will be say three images for each day, and the ID (in the database as "id") will have the day (Sunday, Monday ... Friday, Saturday). Then, I want to display one of them randomly, but only one.

Link to comment
Share on other sites

I put that in and now the error is saying the same but with this instead...

 

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '= 1' at line 1

Couldn't execute query: SELECT * FROM presents WHERE id = 'Friday' LIMIT = 1

Link to comment
Share on other sites

why do you have limit = 1 when you are selecting a ID that you know the id of, you cant have two id's that are the same and besides try LIMIT(1) oh ok bbaker u got it limit 1

 

Cos there are more than one for each day. I only want one to be displayed you see, more would be a right old mess.

Link to comment
Share on other sites

why do you have limit = 1 when you are selecting a ID that you know the id of, you cant have two id's that are the same and besides try LIMIT(1) oh ok bbaker u got it limit 1, you need to do some work on your data structure before you implement a DB

 

What do you mean?

Link to comment
Share on other sites

oh i see what you are doing you have a couple of fridays ?, well this is the wrong way of going about it my man, you dont have same id's, you have different id's and you say that record is a friday by giving it a day feild but u keep the id unique, now you select all the fridays in your

 

SQL SELECT * FROM blah WHERE day = 'friday'

 

then you generate a random number between 1 and 3

 

then you do the loop throgh the result

 

then you stop at the random number you generated

 

by counting the itterations of the loop and using an if and then print it

 

unique ID is a golden rule, this ofcourse depends on you understanding of the feild ID, id is used to uniquley identify a record thats it, any other attribute such as type or what you need DAY is used to group them

 

and use php.net / whatever ur looking for

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.