Hoddz Posted June 29, 2007 Share Posted June 29, 2007 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!! Quote Link to comment Share on other sites More sharing options...
Hypnos Posted June 29, 2007 Share Posted June 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
Hoddz Posted June 29, 2007 Author Share Posted June 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
bbaker Posted June 29, 2007 Share Posted June 29, 2007 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) Quote Link to comment Share on other sites More sharing options...
Hoddz Posted June 29, 2007 Author Share Posted June 29, 2007 Legend!! Have you got to hand the connect that I would need? I know, n00bish, but this will be the only time cos I lost it all as stated above. You would be a little site-saver!! Quote Link to comment Share on other sites More sharing options...
bbaker Posted June 29, 2007 Share Posted June 29, 2007 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."); ?> Quote Link to comment Share on other sites More sharing options...
Hoddz Posted June 29, 2007 Author Share Posted June 29, 2007 That's just the job! Thanks again, means alot to me!! Quote Link to comment Share on other sites More sharing options...
bbaker Posted June 29, 2007 Share Posted June 29, 2007 no problem of course you want to do that before running the query code. Quote Link to comment Share on other sites More sharing options...
Hoddz Posted June 29, 2007 Author Share Posted June 29, 2007 Yeah, I am just editing some of it, to suit and will then paste it here for you to have a quick check over, may make some n00b errors, but we'll soon find out. That is, of course, if you don't mind. Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted June 29, 2007 Share Posted June 29, 2007 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 Quote Link to comment Share on other sites More sharing options...
Hoddz Posted June 29, 2007 Author Share Posted June 29, 2007 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'] . '; } ?> Quote Link to comment Share on other sites More sharing options...
bbaker Posted June 29, 2007 Share Posted June 29, 2007 change this echo ' . $row['code'] . '; to this: echo $row['code']; Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted June 29, 2007 Share Posted June 29, 2007 looks like homework to me ? or did they hire you and you told them you had PHP skill ? Quote Link to comment Share on other sites More sharing options...
Hoddz Posted June 29, 2007 Author Share Posted June 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
Hoddz Posted June 29, 2007 Author Share Posted June 29, 2007 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' Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted June 29, 2007 Share Posted June 29, 2007 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 Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted June 29, 2007 Share Posted June 29, 2007 try LIMIT = 1 coz its a number and put a ; at the end of ur sql statements Quote Link to comment Share on other sites More sharing options...
Hoddz Posted June 29, 2007 Author Share Posted June 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
Hoddz Posted June 29, 2007 Author Share Posted June 29, 2007 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 Quote Link to comment Share on other sites More sharing options...
bbaker Posted June 29, 2007 Share Posted June 29, 2007 remove the = LIMIT 1 Quote Link to comment Share on other sites More sharing options...
Hoddz Posted June 29, 2007 Author Share Posted June 29, 2007 Cheers, all working now!! Now for the random bit. I think it is display(rnd) or something like that. Any ideas? Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted June 29, 2007 Share Posted June 29, 2007 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 Quote Link to comment Share on other sites More sharing options...
Hoddz Posted June 29, 2007 Author Share Posted June 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
Hoddz Posted June 29, 2007 Author Share Posted June 29, 2007 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? Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted June 29, 2007 Share Posted June 29, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.