Jump to content

Echo On Every Html Line Or Is There A Better Solution ?


Lustat

Recommended Posts

is there a way to avoid doing ECHO on every single line of an html page ? I would think heredoc would work, but I cant seem to get it to work. below is the html doc I want to keep as it is, and just enter the php commands where needed.

<?PHP <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>blah blah blah</title><meta http-equiv="Content-Language" content="English" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link rel="stylesheet" type="text/css" href="css file" media="screen" /></head><body><div id="wrap"><div id="header"></div><div id="content"><div class="menu"> <ul><h2>Menu B</h2></div><div class="right"> <center><h2>Body</h2></center><div class="articles">blah blah blahblah blah blahblah blah blahblah blah blahblah blah blah</div></div><div class="left"> <h2>Menu A</h2><ul><li><a href="item b">item a</a></li> <li><a href="item b">item b</a></li> </ul></div><div style="clear: both;"> </div></div><div id="bottom"> </div>					<div id="footer"></div></div></body></html>?>

Link to comment
Share on other sites

Why not just exit the PHP block, write out the HTML then start PHP again?

<?php	//some code?><html code="here"><?php	//more php, etc.?>

Link to comment
Share on other sites

Why not just exit the PHP block, write out the HTML then start PHP again?
<?php	//some code?><html code="here"><?php	//more php, etc.?>

I've tried this and it doesnt work, so I figured it may be an html issue... but now I'm wondering if its php problems
<?php// Make a MySQL Connection$con = mysql_connect("localhost", "username", "pass") or die(mysql_error());mysql_select_db("db_states") or die(mysql_error());// Get all the data from table$result = mysql_query("SELECT state, menuA, menuB FROM states") or die(mysql_error()); // store the record of the "state" table into $row$row = mysql_fetch_array( $result );?>   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title></title><meta http-equiv="Content-Language" content="English" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link rel="stylesheet" type="text/css" href="states.css" media="screen" /></head><body><div id="wrap"><div id="header"></div><div id="content"><div class="menu"> <ul><h2>Menu A</h2><?PHP echo $row['menuA']; ?>

so ya... I'm doing something wrong lol...and the mysql db is set upstates | menuA | menuB | random info | random info 2 | etc

Link to comment
Share on other sites

I've tried this and it doesnt work
Yes it does. If it didn't work for you, then you didn't do it right.Your PHP code looks correct, I don't see any errors with it. What exactly are you seeing when you open the page?
Link to comment
Share on other sites

when you're using $row = mysql_fetch_array ($result) your query should only have up to 1 row.. otherwise you'll have to use mysql_num_rows instead for multiple rowswhat you can try is adding LIMIT 1 at the end of the query

$result = mysql_query("SELECT state, menuA, menuB FROM states LIMIT 1") or die(mysql_error());$row = mysql_fetch_array($result);

Hope it helps!

Link to comment
Share on other sites

You're right - I was just worried about the last line when he used <?PHP echo $row['menuA'];?> where he might have been echoing a row with a blank menuA.

Link to comment
Share on other sites

Yeah, all that would do would be to echo that field from the first row only, whatever the value.
You can echo string literals with PHP:
<?phpEcho('here issome textfor you');?>

Or you could simply use heredocs:

<?phpEcho <<< HEDhere issome textfor youHED;?>

Link to comment
Share on other sites

Yes it does. If it didn't work for you, then you didn't do it right.Your PHP code looks correct, I don't see any errors with it. What exactly are you seeing when you open the page?
here is an exact copy / paste from my page (just some text removed obviously)
<?php// Make a MySQL Connection$con = mysql_connect("localhost", "userid", "pass") or die(mysql_error());mysql_select_db("db1112723_states") or die(mysql_error());// Get all the data from table$result = mysql_query("SELECT state, menuA, menuB FROM states") or die(mysql_error()); // store the record of the "state" table into $row$row = mysql_fetch_array( $result );$state = isset($_GET['state']) ? $_GET['state'] : '';?>   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title></title><meta http-equiv="Content-Language" content="English" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link rel="stylesheet" type="text/css" href="states.css" media="screen" /></head><body><div id="wrap"><div id="header"></div><div id="content"><div class="menu"> <ul><h2>MenuA</h2><?PHP echo {$row['menuA']}; ?>

with this code, I just get a blank white screen...if I move the ?PHP echo {$row['menuA']}; than I see the html page displayed, but no data in it. just the divs and their infoalso all the info in my query is all info from one row, just different columns

Link to comment
Share on other sites

I'm guessing the value for menuA is empty - you can check if it is with:

$test = strlen($row['menuA']);echo 'the length is '.$test;

if it's 0 then it's probably not selecting the right row (you might have other rows in your table)

Link to comment
Share on other sites

I'm guessing the value for menuA is empty - you can check if it is with:
$test = strlen($row['menuA']);echo 'the length is '.$test;

if it's 0 then it's probably not selecting the right row (you might have other rows in your table)

hmm.. went ahead and did
<?php $test = strlen($row['bikemenu']);echo 'the length is '.$test;?>

and it came back with "the length is 0 " which is odd because I do have data in that row... and it is the first and only row...edit: I went ahead and changed the {} to () and now it seems to be working... seems I DID have another field as my first field in the db. one that was all blank.now I'm trying to test out for different states with a URL thats states.php?state=or and that's pulling the info. but if I try states.php?state=whatever it still gives the info for or. isnt it supposed to give me an error ?

Link to comment
Share on other sites

I just noticed the brackets too, those don't belong in an echo statement like that. You could just write this:echo $row['menuA'];

if I try states.php?state=whatever it still gives the info for or. isnt it supposed to give me an error ?
It's not going to do anything you don't tell it to. It's not an error if a query returns no rows, you'll only get a database error if the query has syntax errors. You can use mysql_num_rows to see how many rows a query returned.It sounds like you don't have error messages enabled. Add this to the top of your script:ini_set('display_errors', 1);error_reporting(E_ALL);
Link to comment
Share on other sites

I just noticed the brackets too, those don't belong in an echo statement like that. You could just write this:echo $row['menuA'];It's not going to do anything you don't tell it to. It's not an error if a query returns no rows, you'll only get a database error if the query has syntax errors. You can use mysql_num_rows to see how many rows a query returned.It sounds like you don't have error messages enabled. Add this to the top of your script:ini_set('display_errors', 1);error_reporting(E_ALL);
ya I'm not getting any errors... I think what I need is to create a loop to look through the rows, and once it finds the right one to pull data from it
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...