green917 Posted August 19, 2010 Share Posted August 19, 2010 Hey all, Relative newbie here. I am trying to create a script that generates a random image (and the associated title) from my mySQL database. The code doesn't seem to be working as the image is blank and the title does not appear either. A new set of eyes may be able to find the problem. Thanks! <?php include 'open.php'; include 'opendb.php'; $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS 'offset' FROM 'recipes' "); $offset_row = mysql_fetch_object( $offset_result ); $offset = $offset_row->offset; $result = mysql_query( " SELECT * FROM 'recipes' LIMIT $offset, 1 " ); print "<table width=\"366\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"; print "<tr>"; print "<td class=\"recipe_head\">Healthy Lifestyle Gourmet Recipes</td>"; print "<tr>"; print "<td class=\"recipe_title\">{$result['title']}</td>"; print "</tr>"; print "<tr>"; print "<td><image src=\"{$result['image_path'].$result['image_name']}\" height=\"{$result['image_height']}\" width=\"{$result['image_width']}\" /></td>"; print "</tr></table>"; mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
nblackwood Posted August 19, 2010 Share Posted August 19, 2010 Here, this should help: <?php include 'open.php'; include 'opendb.php'; $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS 'offset' FROM 'recipes' "); $offset_row = mysql_fetch_object( $offset_result ); $offset = $offset_row->offset; $result = mysql_query( " SELECT * FROM 'recipes' LIMIT $offset, 1 " ); print "<table width=\"366\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"; print "<tr>"; print "<td class=\"recipe_head\">Healthy Lifestyle Gourmet Recipes</td>"; print "<tr>"; print "<td class=\"recipe_title\">{$result['title']}</td>"; print "</tr>"; print "<tr>"; print "<td><image src=\"{".$result."['image_path']".$result."['image_name']}\" height=\"{".$result."['image_height']}\" width=\"{".$result."['image_width']}\" /></td>"; print "</tr></table>"; mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
DavidAM Posted August 19, 2010 Share Posted August 19, 2010 It would help if you indicated which line the error was reported on. I think your problem might be in this line: print "<td><image src=\"{$result['image_path'].$result['image_name']}\" height=\"{$result['image_height']}\" width=\"{$result['image_width']}\" /></td>"; I'm not sure, but I don't think you can use the dot operator inside the curly-braces. I'm not sure, because I almost never use curly-braces. This type of statement is very difficult to read when working with your code, and prone to errors. There are a couple of different ways to clean it up. First, instead of escaping double quotes inside a double-quoted string, switch to single quotes for one or the other. My favorite method is to use the printf() function: printf('<td><image src="%s" height="%d" width="%d" /></td>', $result['image_path'] . $result['image_name'], $result['image_height'], $result['image_width']); I have switched to using printf() and sprintf() for HTML and SQL generation almost entirely. The reason is clear. If you look at that code, you can see the HTML you are going to generate with the placeholders for the variables, then the variables are listed in the order they are applied. Or use single quotes for the print and concatenate the variables: print '<td><image src="' . $result['image_path'] . $result['image_name'] . '" height="' . $result['image_height'] . '" width="' . $result['image_width'] . '" /></td>'; If you really like the curly-braces, I think the correct way would be to remove the "." between the image_path and image_name and replace it with more curly-braces (they are separate varaibles): print "<td><image src=\"{$result['image_path']}{$result['image_name']}\" height=\"{$result['image_height']}\" width=\"{$result['image_width']}\" /></td>"; Quote Link to comment Share on other sites More sharing options...
green917 Posted August 19, 2010 Author Share Posted August 19, 2010 Thanks so much for the help! 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.