954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

html table in php code.

Dear friend,

I can't find the syntax error in my html table in php code.

echo "<table border='1'>";
	echo"<tr>";
		echo"<th>".{$data['engword']}."</td>";
		echo"<th align='left'> "<a href=\"http://www.domain.com/sound/En-us-{$data['engword']}.WAV \" title=\"open the audio file for the word {$data['engword']} \"rel='dbPopWin'><img src='audio.gif'></a>"."</td>";
	echo"</tr>";
	echo"<tr>";
		echo"<td align='left'></td>";
		echo"<td>".(stripslashes($data["mword"]))."</td>";
	echo"</tr>";
	
echo"</table>";


many thanks in advance,

regards.
ko

sayakyi
Newbie Poster
17 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

Error is:

echo"<th>".{$data['engword']}."</td>";


and you don't need () around the stripslashes function.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

With so much html and so little php, perhaps you'd be better off 'reversing' the coding. Your code:

echo "<table border='1'>";
	echo"<tr>";
		echo"<th>".{$data['engword']}."</td>";
		echo"<th align='left'> "<a href=\"http://www.domain.com/sound/En-us-{$data['engword']}.WAV \" title=\"open the audio file for the word {$data['engword']} \"rel='dbPopWin'><img src='audio.gif'></a>"."</td>";
	echo"</tr>";
	echo"<tr>";
		echo"<td align='left'></td>";
		echo"<td>".(stripslashes($data["mword"]))."</td>";
	echo"</tr>";
	
echo"</table>";


You've gotta lot of xhtml errors above (e.g. content inside and ; single quotes around attribute values instead of doubles.)

Change this to:

<table border="1">
 
...include <thead> bits...

    <tbody>
<?php
    while($data = mysql_fetch_array('result'){
?>
        <tr>
            <td><?=$data['engword'];?></td>
	    <td align="left"><a href= "http://www.domain.com/sound/En-us-<?={$data['engword']};?>.wav" title="open the audio file for the word <?=$data['engword'];?>" rel="dbPopWin"><img src= "audio.gif"></a></td>
        </tr>
	<tr>
            <td align="left">&nbsp;</td>
            <td><?=stripslashes($data["mword"]);?></td>
	</tr>
<?php
    }
?>
    </tbody>
</table>


The '<?=' is shorthand for '<?php echo '. This may be frowned upon by some developers, but if your setup supports short tags (it should), then it's a time saver.

Again many developers would frown at mixing xhtml and raw php code like this, but what the hell? You're only creating a basic link list.

I think that keeping to 'normal' xhtml as much as possible and just inserting the odd bit of 'php echo code' can see you OK. Once you start with all those \" escaped quotes, things can become very messy.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Where does it say that you have to use double quotes? I ran a check through w3.org validator and there were no xhtml errors concerning single quotes.

I am one of those developers who frowns upon mixing html and php (and using short tags). I suggest you use heredoc syntax.

echo <<

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

thanks you for your hint with xhtml and php syntax guide.

It is embarrassing to ask you again.., but I need to know how I can better clean my code.
I dont have still result from my search.php

search.php

<body>
<table border="1">
<?php
$db_name="dic";

trim($searchterm);
if (!$HTTP_GET_VARS["searchterm"])
{
echo "You haven't entered any word to search.Please, go back and entered it.";
exit;
}
$searchterm = addslashes($searchterm);
require("dict.php");
if (!$db)
{
echo "Error. Can't connect to database.Please try later";
}
mysql_select_db($db_name);
$query = "select * from dic where engword like '".$HTTP_GET_VARS["searchterm"]."%' order by engword";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
?>
<tbody>
<?php

	   while($data = mysql_fetch_array('result')){
?>
		
	        <tr>
	            <td><?=$data['engword'];?></td>
		    <td align="left"><a href= "http://www.domain.com/sound/En-us-<?={$data['engword']};?>.wav" title="open the audio file for the word <?=$data['engword'];?>" rel="dbPopWin"><img src= "audio.gif"></a></td>
	        </tr>
		<tr>
	            <td align="left">&nbsp;</td>
	            <td><?=stripslashes($data["mword"]);?></td>
		</tr>
	<?php
	    }
	?>
	    </tbody>
</table>
<?php
echo mysql_close($db);
?>
<a href='index.php'>search another word</a>
</body>
</html>


best regards,

koko

Attachments search.php (1.54KB)
sayakyi
Newbie Poster
17 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

@KK frown away!

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Damn! Meant to edit the last post.
@KK
Sorry KK, that sounded a bit rude. The heredoc syntax, I will concede is a better way if there's loads of text/html and the occasional php variable to insert. However, if you need to stripslash or whatever, it gets a bit messy, or you have to transform before the heredoc and then insert the vars.

@Say
Sorry fella, I'm a bit busy at the moment. I don't think you're far off with that code. Just play with it, try to isolate errors, change single lines at a time, etc.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

if you code's most of the part is html then you should use php with in html tags but if your php part is more then html then use html in php qoutes.

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

There are a few web pages setup to do this for us... here is one of them:
http://corz.org/machine/source/php/tag-tools.php

beeerlover
Light Poster
40 posts since May 2009
Reputation Points: 10
Solved Threads: 6
 

That's nice, especially for newbies. However, I think we're starting to wander off the point for Sayakyi. Any luck yet?

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

I just optimized(?) my code. But still no result.:'(

<?php
	$host="localhost"; // Host name
	$username="mydic"; // Mysql username
	$password="secret"; // Mysql password
	$db_name="mydic"; // Database name
	$tbl_name="dic"; // Table name

	// Connect to server and select database.
	mysql_connect("$host", "$username", "$password")or die("cannot connect");
	mysql_select_db("$db_name")or die("cannot select DB");

	
	trim($searchterm);
	if (!$HTTP_GET_VARS["searchterm"])
	{
	echo "You haven't entered any word to search.Please, go back and entered it.";
	exit;
	}


	$query = "select * from dic where engword like '".$HTTP_GET_VARS["searchterm"]."%' order by engword";
	$result = mysql_query($query);
	$num_results = mysql_num_rows($result);
	echo "<b>".$HTTP_GET_VARS["searchterm"]."</b>"."&nbsp;"."found in database:"."&nbsp;"."<b>".$num_results."</b>"."&nbsp;". "" ."</p>";

?>


<table>
<tbody>
<?php

	   while($data = mysql_fetch_array('$result')){
?>
		
	        <tr>
	            <td><?=$data['engword'];?></td>
		    <td align="left"><a href= "http://www.domain.com/sound/En-us-<?={$data['engword']};?>.wav" title="open the audio file for the word <?=$data['engword'];?>" rel="dbPopWin"><img src= "audio.gif"></a></td>
	        </tr>
		<tr>
	            <td align="left">&nbsp;</td>
	            <td><?=stripslashes($data["mword"]);?></td>
		</tr>
	<?php
	    }
	?>
	    </tbody>
</table>
  
</html>


many thanks in advance,

saya

sayakyi
Newbie Poster
17 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You