Forum Moderators: coopster
Without knowing too much about the actual interchange of php and mysql, I searched the net, and came up with NOTHING that could do what I wanted... so I hit the books and this is what I came up with...
<?php
//open connection
$conn = mysql_connect ("host", "user", "pass");
//pick database
mysql_select_db("xfdesign_digdiva", $conn);
//create sql Statement
$query_siteImg = "SELECT * FROM siteimages WHERE siteimages.imgID = 1";
//execute sql Statement
$result = mysql_query($query_siteImg, $conn) or die(mysql_error());
//get rows
$row = mysql_fetch_array($result);
//output Image
echo "<img src=\"includes/images/".$row['imgFilename']."\" alt=\"".$row['imgTitle']."\">";
?>
This really works for what I want it to do for now which is to be placed wherever I need a specific image... my request is this....
How can I slim this up, and can it be simplified to a function, or variable that is called from an external file? Most important question is how to do this and still be able to specify the imageID for this to work.
Your Thoughts?
Thanks
Tim
<?php
//function.php
function displayImage($imageID) {
//open connection
$conn = mysql_connect ("host", "user", "pass");
//pick database
mysql_select_db("xfdesign_digdiva", $conn);
//create sql Statement
$query_siteImg = "SELECT * FROM siteimages WHERE siteimages.imgID = ".$imageID;
//execute sql Statement
$result = mysql_query($query_siteImg, $conn) or die(mysql_error());
//get rows
$row = mysql_fetch_assoc($result);
//output Image
echo "<img src=\"includes/images/".$row['imgFilename']."\" alt=\"".$row['imgTitle']."\">";
}
?>
<?php
//calling file
//import the function file
require_once('function.php');
//call the function, passing the imageID
displayImage(1);
?>
As an aside, unless you are doing this as an exercise, it would probably be advisable to find an alternative method to showing web-graphics. By doing it like that you will probably be making many queries to the database per page load which is quite inefficient when there are simpler ways..
Hope that helps
[edited by: darrenG at 11:17 pm (utc) on June 26, 2007]
... on a similar note... what would be some better ways of handeling this. My main idea is to create modular sites. I am a visual designer first, coder... well about 5th or 6th. I am attempting to make a framework that will allow me to customize images for sites... on the fly, if you will. With that in mind, my idea was to use the database to store all site image filenames, then recall them at their specific locations.
Another thought I had was to have a static image filename, and re-write that image each time... but that does no good if you want to change the alt text...
Your thoughts?
btw... Thanks for your help!
However, instead of calling names from a database, the function just spews out html.
I just re-read your original question and think I might have missed your point originally, so Im going on the following assumptions:
The images you will be working with (header, nav etc) wont be changing too frequently.
You dont want users who are not savvy with working with code to be able to change the site graphics.
If those assumptions are wrong, your original method might well be better...
Anyhow, a page might look like the following:
<?php
//import functions
require_once('page.inc');
$pageTitle = 'Welcome Home';
$pageDescription = 'This is the Home page, we make and sell widgets';
$keywords = 'welcome, home, widgets';
//show the page header
displayHeader($pageTitle, $pageDescription, $keywords);
//body
displayFooter();
?>
<?php
//page.inc
function displayHeader($title = '', $description = '', $keywords = '') {
?>
<html>
<head>
<title><?php print($title);?>...
<?php
//...
?>
<div>
<img src="logo.png" alt="Widget Makers">...
<?php
//...
}
function displayFooter() {
//...
}
?>
That way, when you need to change an image that applies to many pages, you just change it in the function. Same with any html.
My original idea was to set the site up so that eventually I could create an administrative login, lock out the pages based on access level, and allow the users to change the images thenselves... as I've been thinking, it may be better to create a form that instead of populating a database with the image locations, actually outputs the "page.inc" this should sufficiently eliminate any extra server overhead.
what would be some pro's / con's to this approach vs. storing the image filenames and pulling them dynamically?
I dunno it just seems excessive to be calling images from the database, when you will also be authenticating users, and pulling content from the DB too. You could have three or four calls to the DB before any text based content has even been delivered.... on every single page!
Pros and cons of writing to files vs database, hmm well, writing to files can be slightly more complicated to code, and may take a bit longer to actually process the changes. But, calling code from the file is a lot more efficient once any updates have been made.
So writing to files:
- slightly more complicated to code
- not as easy to implement
- slower performing updates
- quicker page loads particularly if the site is busy
DB:
- more straight-forward
- easier to implement
- quicker updates
- slower page loads particularly when under load
Also, I guess it depends on how you structure your database.
You know what, Im starting to think Im talking nonsense lol - go ahead and do it your original way, see how you get on :)
Can anyone else shed any more light on the subject?
Extremely new here (1st post!) and I must say firstly I think this site is a great source of information and help. So others may disagree with what I suggest below but hey if they do and tell me why I'll be learning as well ;)
Just read your post and this is what I would suggest doing (on an overall scale)
Create a seperate config.php file located above the site root if you can, especially if you will be making lots of calls to the db and include this in your header page.
Instead of allowing the users to change every image why not just create certain themes/styles that all have different images and allow the user to change what stylesheet they use (This info could be grabbed form the db)
e.g
$q = mysql_query("SELECT * FROM users WHERE username ='$username'");
while($r = mysql_fetch_array($q)){
$style = $r['style'];
}
echo "<link rel=\"stylesheet\" href=\"themes/$style/style.css\" type=\"text/css\" />";
One other thing I would look into is .inc files. Either make sure these are included above the site root, or are named .inc.php as .inc will output your php code (if a user accesses directly) to the browser and someone may be able to use this code for malicious purposes ( i believe this is correct).
As I say this is what I've picked up on my travels around the web but it would be interesting to see how others would approach this.
Good Luck
"Instead of allowing the users to change every image why not just create certain themes/styles that all have different images and allow the user to change what stylesheet they use (This info could be grabbed form the db)"
and now for the "Rest" of the story...
My issue isn't one of coding, in as much as it is an issue of being married to an anylitical chemist... who is also a jewelry designer and works with photoshop!....
My wife is amazing with her ability to handle volitile substances, without blowing herself up, and then turn around and make some of the most delicate pieces of jewelry you have ever seen.... however, here lie's a bit of an issue...
she learned how to use photoshop CS (very well I might add) in a matter of 1 month, and uses it to edit and enhance her jewelry pics, as well as help me visually design for my company.... Naturally she wants her own site, and wants it to have all of the features of other major jewelry sites, and wants it all in my freetime, last week (don't get me wrong... it gives me great opportunity to expand my knowledge... and I am all for that) Problem is... she refuses to learn web coding, or anything else like that. In my effort to be a good husband, as well as an even better designer, I have set upon this quest. Her current site (listed in my profile) has an admin section which will allow her to change anything she wants in her schedule... which for anyone that is married will probably agree, that alone is worth it's weight in gold!
I created the site using dreamweaver and the mxkollections extensions, now adobe design toolbox. Works Great... however if you have ever used those extensions, you know to customize finctions you have to wade through about 1000 propriatary files and risk the adobe poiice coming after you if you accidentaly change the code and forget to put the correct copyright date on a function... SUPER BLOATED!
In an effort to expand my options to my clients (And appease my wife) I came upon this idea,,,, basically a re-write of their "Show Dynamic Images" behavior, but allowing me to take it one step further and show a specific dynamic image in a specific location... what I have now.
This is my quest, and ultimately I am planning on the quest allowing my wife to add her own created images into her site design, one way or another, without her ever seeing one piece of code.....
whew... that's a mouthfull!
So far, You have all given me some great advice, that I will use for other clients...
and if you or anyone else has any other suggestions to my particular delima, I am most appreciative!
Thanks guys! (and gals, if you are reading this)
other thoughts?
tim
*I got married 28 years ago, the happiest moment of my life... 29 years ago (walter the puppett, via. jeff dunham - arguing with myself)*
I kind of follow what you're saying. Why not just create an image upload form then so she can upload images. Whatever she then uses to edit a page in the backend there could be a pop-up box that lists all the images she has uploaded (latest first) with an id. Then all she would have to do is add a similar piece of code to bb style code [ img = 14 ] or whatever and you then run a preg_replace on that replacing it with an img tag for that id? Everytime she uploads a pic you could also make sure she enters a title/brief description which could then be used for title/alt tag?
Alternatively if the images will always be in specific places on the page. Number these places up and have a form where the image id can be edited via the admin side.
btw looked at your profile but couldn't see the link?
currently she does use an upload form,, it just uploads the file to a folder and puts the image name in a database table
I guess, to me your idea seems like it has more steps than are needed for her, as well as any end user...
Ideal situation in my oppinion(sp?) for my wife as well as any end user/client would be for them to go to one page and be able to update/change their info/pics, etc by just changing pertinant text (if necessary) or uploading a new pic and clicking submit, that's all... again ideal... even more ideal for me is to accomplish this framework as simply as I can without alot of bloated code and still be secure
I know, only in a perfect world... but one can hope!