clickable $row array elements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

clickable $row array elements

Post 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
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

replace ',' with '.' in your echo statment.
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post 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
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Try putting your variables that are in the string in curly braces like

echo "some stuff{$var} more stuff {$array['index']}" ;
User avatar
dxm
Forum Newbie
Posts: 8
Joined: Fri Jun 29, 2007 5:18 pm
Location: Wales

Post 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 />'; 
} 
?>
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

it worked. thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Begby wrote:replace ',' with '.' in your echo statment.
Interestingly, the former is perfectly legal and recommended. :)
Post Reply