Page 1 of 1

clickable $row array elements

Posted: Mon Jul 16, 2007 10:38 am
by m2babaey
Hi
I want the titles of the listed articles clickable using this code:

Code: Select all

<?php
include'global.php';
$result=mysql_query("SELECT * FROM articles ORDER BY id DESC LIMIT 20");
?>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p align=right>
<?
while( $row=mysql_fetch_array($result))  {
  $id=$row['id']; 
  echo "<a href=\"readarticle.php?id=$id\">$row['title']</a>" ,'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', '&#1606;&#1608;&#1588;&#1578;&#1607;','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $row['name']   , "<br />";
}
?>
and get this error:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in g:\programs\easyphp1-8\www\takfekr\htdocs\home.php on line 13

Posted: Mon Jul 16, 2007 11:12 am
by Begby
replace ',' with '.' in your echo statment.

Posted: Mon Jul 16, 2007 11:37 am
by m2babaey
this code:

Code: Select all

<?php
include'global.php';
$result=mysql_query("SELECT * FROM articles ORDER BY id DESC LIMIT 20");
?>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p align=right>
<?
while( $row=mysql_fetch_array($result))  {
  $id=$row['id']; 
  echo "<a href=\"readarticle.php?id=$id\">$row['title']</a>" .'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'. 'by'.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'. $row['name']   . "<br />";
}
?>
is sending:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in g:\programs\easyphp1-8\www\takfekr\htdocs\home.php on line 13
too

Posted: Mon Jul 16, 2007 12:01 pm
by Begby
Try putting your variables that are in the string in curly braces like

echo "some stuff{$var} more stuff {$array['index']}" ;

Posted: Mon Jul 16, 2007 12:13 pm
by dxm
Try:

Code: Select all

<?php 
include'global.php'; 
$result=mysql_query("SELECT * FROM articles ORDER BY id DESC LIMIT 20"); 
?> 
<html> 
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
</head> 
<body> 
<p align=right> 
<? 
while( $row=mysql_fetch_array($result))  { 
  $id=$row['id']; 
  echo '<a href="readarticle.php?id=' . $id . '">' . $row['title'] . '</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;by&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $row['name']   . '<br />'; 
} 
?>

Posted: Mon Jul 16, 2007 1:01 pm
by m2babaey
it worked. thanks

Posted: Mon Jul 16, 2007 1:07 pm
by RobertGonzalez
Your problem was with the attempt to parse $row['title']. In the case of your original code, you would have either had to have removed the quotes around the array index or wrapped the entire var in curly braces.

Posted: Mon Jul 16, 2007 10:43 pm
by feyd
Begby wrote:replace ',' with '.' in your echo statment.
Interestingly, the former is perfectly legal and recommended. :)