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

Error is:

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

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

Member Avatar for diafol

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 <th> and </td>; 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.

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 <<<HTML
//put html here use {$var} to include variables.
HTML;

That way you can write html code like normal and it still uses php.

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

Member Avatar for diafol

@KK frown away!

Member Avatar for diafol

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.

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.

Member Avatar for diafol

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.