kiwisi Posted July 22, 2013 Share Posted July 22, 2013 So I have this code working fine and it shows almost everything I need. But I want the "bizname" to show in the title tag. I tired echoing the bizname in the title tag but it doesn't do anything. Everything else I look for is about making separate header pages but maybe I'm not searching for the right term? <?php $dbc = mysqli_connect('localhost','','','') or die ('Error connecting to the database'); $RID = $_GET['bizid']; $query="SELECT * FROM business WHERE `bizid` = '$RID'"; $data=mysqli_query($dbc,$query); if($data->num_rows == 0){ echo 'Sorry, there are no matches'; } else { echo '<table>'; while ($row = mysqli_fetch_array($data)){ echo '<tr border="1"> <td class="td1"><h1>' .$row['bizname'] . '</h1></td>'; echo '</tr>'; echo '<tr> <td class="td3">' .$row['bizhours'] . '</td>'; echo '</tr>'; echo '<tr><td class="td2">' .$row['bizdescription'] .'</td>'; echo '</tr>'; echo '<tr> <td class="td2">' .$row['bizaddress'] . '</td>'; echo '</tr>'; echo '<tr> <td class="td2">Phone: ' .$row['bizphone'] . '</td>'; echo '</tr>'; echo '<tr> <td class="td2">Fax: ' .$row['bizfax'] . '</td>'; echo '</tr>'; echo '<tr> <td class="td2">Email: ' .$row['bizemail'] . '</td>'; echo '</tr>'; echo '<tr><td></td>'; echo '</tr>'; } echo '</table>'; } mysqli_close($dbc); ?> Quote Link to comment https://forums.phpfreaks.com/topic/280391-get-record-name-into-title/ Share on other sites More sharing options...
Muddy_Funster Posted July 22, 2013 Share Posted July 22, 2013 (edited) the page title is defined in the html header. If you want to change it you need to write out the whole page, including the header, when you are displaying the results of the query - so else{ $row = mysql_fetch_assoc($data); $html = <<<HTML <!DOCTYPE html> <html lang="en"> <head> <title> {$row['bizname']} </title> </head> <body> <table> <tr border="1"> <td class="td1"><h1>{$row['bizname']}</h1></td> </tr> <tr> <td class="td3">{$row['bizhours']}</td> </tr> <tr> <td class="td2">{$row['bizdescription']}</td> </tr> <tr> <td class="td2">{$row['bizaddress']}</td> </tr> <tr> <td class="td2">Phone : {$row['bizphone']}</td> </tr> <tr> <td class="td2">Fax : {$row['bizfax']}</td> </tr> <tr> <td class="td2>Email : {$row['bizemail']}</td> </tr> <tr> <td></td> </tr> </table> </body> </html> HTML; echo $html; You can't loop multiple results, as you can only have 1 title per page. Edited July 22, 2013 by Muddy_Funster Quote Link to comment https://forums.phpfreaks.com/topic/280391-get-record-name-into-title/#findComment-1441648 Share on other sites More sharing options...
kiwisi Posted July 22, 2013 Author Share Posted July 22, 2013 (edited) Is that how most pages do it - or am I making it hard for myself? Bearing in mind - I do have all the appropriate html tags before the php script i listed.... Edited July 22, 2013 by kiwisi Quote Link to comment https://forums.phpfreaks.com/topic/280391-get-record-name-into-title/#findComment-1441652 Share on other sites More sharing options...
Muddy_Funster Posted July 22, 2013 Share Posted July 22, 2013 you can't - as far as I am aware - change the header with PHP once it's been set without rendering the entyre page again. You could maybe get away with calling header('Title: '.$row['bizname']); But that's untested and I would say unlikely as well. The way PHP works it that it is processed on the server before any information is displayed in the browser, this meens that all PHP code on a page is run through before a single element is shown on the screen (unless you use something like output buffers), to change things after the page has been "drawn" then you need something client side, like javascript. You could theoreticly have javascript/jquery change the DOM for the page title - but again, that's not something I have ever played about with. Quote Link to comment https://forums.phpfreaks.com/topic/280391-get-record-name-into-title/#findComment-1441655 Share on other sites More sharing options...
kiwisi Posted July 25, 2013 Author Share Posted July 25, 2013 Cheers muddy So - without sounding like a broken record....(cd to you younger peeps) how then, do such seemingly simple sites get a record name fro mysql (like in my instance, bizname) into the title when parsing from another page? Like, "click here for this cafe", then the cafe name is in the page title.. I keep thinking I'm missing a trick here somewhere... Quote Link to comment https://forums.phpfreaks.com/topic/280391-get-record-name-into-title/#findComment-1442092 Share on other sites More sharing options...
Muddy_Funster Posted July 25, 2013 Share Posted July 25, 2013 I've had a quick poke about and you can do it with JQUERY using .arr() like so: function changeTitle(newTitle){ $(document).attr("title", newTitle); } It's not something I have used in production, but I did test that and it works. Quote Link to comment https://forums.phpfreaks.com/topic/280391-get-record-name-into-title/#findComment-1442108 Share on other sites More sharing options...
mac_gyver Posted July 25, 2013 Share Posted July 25, 2013 (edited) you need to rearrange the logic on your page so that the main (business logic) php code is executed first, then you build and output the html document using the data that php produced/retrieved. for the code in post #1 in this thread, that would mean replacing the echo statements with code that concatenates the output into a php variable. you would then simply echo the php variable holding the page title and the php variable holding the main page content at the appropriate points in your html document. all the html document, from the <!DOCTYPE html> tag to the closing </html> tag, would come after the main php code on the page. edit: organizing the logic on your page the way that i have described (i.e. using php as a template engine) also eliminates a common problem like in this thread - http://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/ Edited July 25, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/280391-get-record-name-into-title/#findComment-1442134 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.