joshgarrod Posted January 22, 2009 Share Posted January 22, 2009 Hi, i have spent hours on google looking for an anwser as I just cant seem to get this to work. It seemed simple at first, but now I am not too sure. I am displaying information of a single row on a page using a while loop. I would like to take 4 variables $make $model $year and $county to use in the title tag. This is what I have so far and it just shows me a totally blank page, it works without it it fine, where am I going wrong? <title><?php custom_title(); ?></title> <?php if(mysql_num_rows($query)==0){ die("Sorry, but the page doesnt exist!"); }else{ while($info = mysql_fetch_array($query)){ $price = number_format($info ['price']); //formatting the price to use a thousand separator echo "<div id=\"details\">"; echo "<div id=\"make\"><h1>" . $info['make'] . " " . $info['model'] . "</h1></div>"; echo "<div id=\"left_spec\">"; echo " <span class=\"price\">£$price</span>"; echo "<h2>Specification:</h2>"; echo "<div class=\"spec_item\"><strong>Year: </strong></div><div class=\"spec_detail\">" . $info ['year'] . "</div>"; echo "<div class=\"spec_item\"><img src=\"images/".$info['berth'] . ".gif\" class=\"berth\" alt=\"". $info['berth'] ." berth\"/>" .$info['berth'] . " berth</div>"; echo "<div class=\"spec_item\"><strong>MIRO: </strong></div><div class=\"spec_detail\">".$info['weightun'] . "kg</div>"; echo "<div class=\"spec_item\"><strong>MTPLM: </strong></div><div class=\"spec_detail\">".$info['weightla'] . "kg</div>"; echo "<div class=\"spec_item\"><strong>Internal: </strong></div><div class=\"spec_detail\">".$info['lengthin'] . "ft</div>"; echo "<div class=\"spec_item\"><strong>Shipping length: </strong></div><div class=\"spec_detail\">".$info['lengthex'] . "ft</div>"; echo "<div class=\"spec_item\"><strong>Awning size: </strong></div><div class=\"spec_detail\">".$info['awning'] . "cm</div>"; echo "<div class=\"spec_item\"><strong>Additional description: </strong></div><div id=\"additional\">".$info['spec'] . "</div><br />"; echo "<h2>Contact:</h2>"; echo "<div class=\"spec_item\"><strong>Contact name: </strong></div><div class=\"spec_detail\">".$info['contactname'] . "</div>"; echo "<div class=\"spec_item\"><strong>Telephone: </strong></div><div class=\"spec_detail\">".$info['contactnum'] . "</div>"; echo "<div class=\"spec_item\"><strong>Email: </strong></div><div class=\"spec_detail\"><a href=\"mailto:".$info['contactemail'] . "\">".$info['contactemail'] . "</a></div>"; echo "<div class=\"spec_item\"><strong>County: </strong></div><div class=\"spec_detail\">".$info['county'] . "</div><br /><br />"; echo "<hr width=\"90%\" class=\"solid\">"; echo "<form method='post' action='sendmail.php'> <div class=\"form_titles\">Name: </div><input name=\"name\" type=\"text\" /><br /> <div class=\"form_titles\">Email address: </div><input name=\"email\" type=\"text\" /><br /> <input name=\"hidden\" type=\"hidden\" value=\"". $info ['contactemail'] ."\" /> Enquiry:<br /> <textarea name=\"comments\" rows=\"15\" cols=\"40\"> </textarea><br /> <input type=\"submit\" /> </form>"; echo "</div>"; echo "<div id=\"right_images\">"; echo "<img src=\"".$info['image2'] . "\" class=\"display\" alt=\"". $info['make'] . " " . $info['model'] ."\"/>"; echo "<img src=\"".$info['image3'] . "\" class=\"display\" alt=\"". $info['make'] . " " . $info['model'] ."\"/>"; echo "<img src=\"".$info['image4'] . "\" class=\"display\" alt=\"". $info['make'] . " " . $info['model'] ."\"/>"; echo "<img src=\"".$info['image5'] . "\" class=\"display\" alt=\"". $info['make'] . " " . $info['model'] ."\"/>"; echo "</div>"; echo "</div>"; function custom_title() { $make = $info ['make']; $model = $info ['model']; $year = $info ['year']; $county = $info ['county']; echo "Used $make $model $year for sale in $county"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/141913-solved-dynamically-generated-page-titles/ Share on other sites More sharing options...
Mchl Posted January 22, 2009 Share Posted January 22, 2009 Read on variable scope. In short: variables declared outside of a function are not seen within it. You have to pass them to it. Another thing. It's genrally better to have a function that returns a value, and then echo it. function custom_title($info) { $make = $info ['make']; $model = $info ['model']; $year = $info ['year']; $county = $info ['county']; return "Used $make $model $year for sale in $county"; } <title><?php echo(custom_title($info)); ?></title> Quote Link to comment https://forums.phpfreaks.com/topic/141913-solved-dynamically-generated-page-titles/#findComment-743040 Share on other sites More sharing options...
joshgarrod Posted January 22, 2009 Author Share Posted January 22, 2009 Hi, thanks v much for the reply, I have done what you suggested and replaced my function and title tag with what you suggested but I am still getting a blank page? is it because the function is inside the loop which is after the title tag? Quote Link to comment https://forums.phpfreaks.com/topic/141913-solved-dynamically-generated-page-titles/#findComment-743042 Share on other sites More sharing options...
dropfaith Posted January 22, 2009 Share Posted January 22, 2009 add this to the top of your php // Report all PHP errors error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/141913-solved-dynamically-generated-page-titles/#findComment-743043 Share on other sites More sharing options...
Mchl Posted January 22, 2009 Share Posted January 22, 2009 At the point where you do: <title><?php echo(custom_title($info)); ?></title> you have to have $info filled with data, and custom_title() function declared. Quote Link to comment https://forums.phpfreaks.com/topic/141913-solved-dynamically-generated-page-titles/#findComment-743044 Share on other sites More sharing options...
joshgarrod Posted January 22, 2009 Author Share Posted January 22, 2009 is there any way i can get around it because as you can see by my code that the loop creates the page which obviously must come after the title tag, I have turned error reporting on and still a blank page? Quote Link to comment https://forums.phpfreaks.com/topic/141913-solved-dynamically-generated-page-titles/#findComment-743047 Share on other sites More sharing options...
Mchl Posted January 22, 2009 Share Posted January 22, 2009 is there any way i can get around it because as you can see by my code that the loop creates the page which obviously must come after the title tag, I have turned error reporting on and still a blank page? There is. It's called separation of data preparation and data display. You should run all mysql queries that get data from database first. Then display your HTML. Quote Link to comment https://forums.phpfreaks.com/topic/141913-solved-dynamically-generated-page-titles/#findComment-743048 Share on other sites More sharing options...
joshgarrod Posted January 22, 2009 Author Share Posted January 22, 2009 awesome got it working, thanks so much for your help Quote Link to comment https://forums.phpfreaks.com/topic/141913-solved-dynamically-generated-page-titles/#findComment-743049 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.