Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call public function article() inside printf()

Status
Not open for further replies.

pheng56

IS-IT--Management
Feb 24, 2013
7
CA
Hello,

Please assist me to fix this calling function within printf();

I really appreciate,
Thank you in advance,
Best regards,

Code:
public function main()
	
	{
	
	
	   // SQL to display the article part with number of comments and last date time of comments  
       $sql = "SELECT a.ID,a.title,a.content,a.timestamp,u.username, c.total_comment, c.lastcommentdate FROM article AS a LEFT JOIN user AS u on a.userID = u.ID 
       LEFT JOIN(SELECT articleID, COUNT(ID) as total_comment, MAX(timestamp) as lastcommentdate FROM comment GROUP by articleID) 
       AS c on a.ID = c.articleID ORDER BY a.ID DESC LIMIT 5";

       $result = mysql_query($sql);
       if (!$result)
       	die("SQL error: ".$sql." ERROR: ".mysql_error());

	while ($row = mysql_fetch_assoc($result)) {
        	printf("<hr><h3><a href=\"article()?id=%d\">%s</a></h3><b>Author %s, date %s</b><p>**%s**</p><b>%s</b> Comments and the last comment posted on %s<br> ",$row['ID'], 
	       $row['title'], $row['username'],$row['timestamp'], substr($row['content'],0,100),$row['total_comment'], $row['lastcommentdate']);	
        }
	}
	
	
	public function article()
	{
	
		$id=0;  
        // to get a ID number that come from the chosen article 
        if(isset($_REQUEST['id'])) {
        	$id = intval($_REQUEST['id']);  
        }  
     	// to display the comments related to the chosen article
        	$sql = 'SELECT a.ID, c.content,c.timestamp, u.username as comment_author FROM article AS a INNER JOIN comment AS c ON a.ID = c.articleID INNER JOIN user as u ON u.ID = c.userID WHERE a.ID = "' . $id . '" ORDER BY c.timestamp DESC LIMIT 5'; 

        	$result = mysql_query($sql);
	        if (!$result)
        		die("SQL error: ".$sql." ERROR: ".mysql_error());

	     	while ($row = mysql_fetch_assoc($result)) {
	    	printf("<hr><p>%s</p><b>Author %s, Date %s</b><br><br>",$row['content'],$row['comment_author'], $row['timestamp']);	
	    }
	}
 
you have not said what is not working. so it is difficult to fix it.

you may find it helpful to give lots more whitespace to your coding style. spread things out to make it easier to spot errors. this helps with debugging and also revisiting code later.

i have rewritten some bits of your code but did not spot any obvious mistakes.

Code:
[COLOR=#990000]<?php[/color] 
public [b][COLOR=#0000FF]function[/color][/b] [b][COLOR=#000000]main[/color][/b][COLOR=#990000]()[/color] [COLOR=#FF0000]{[/color]
[tab][i][COLOR=#9A1900]// SQL to display the article part with number of comments and last date time of comments[/color][/i]
[tab][COLOR=#009900]$sql[/color] [COLOR=#990000]=[/color] [COLOR=#990000]<<<[/color]SQL
SELECT[tab]  a[COLOR=#990000].[/color]ID [b][COLOR=#0000FF]as[/color][/b] ID[COLOR=#990000],[/color] a[COLOR=#990000].[/color]title [b][COLOR=#0000FF]as[/color][/b] title[COLOR=#990000],[/color]a[COLOR=#990000].[/color]content [b][COLOR=#0000FF]as[/color][/b] content[COLOR=#990000],[/color]a[COLOR=#990000].[/color]timestamp [b][COLOR=#0000FF]as[/color][/b] timestamp[COLOR=#990000],[/color]u[COLOR=#990000].[/color]username [b][COLOR=#0000FF]as[/color][/b] username[COLOR=#990000],[/color] 
[tab][tab][tab]c[COLOR=#990000].[/color]total_comment [b][COLOR=#0000FF]as[/color][/b] total_comment[COLOR=#990000],[/color] c[COLOR=#990000].[/color]lastcommentdate [b][COLOR=#0000FF]as[/color][/b] lastcommentdata 
FROM[tab][tab]article  a 
LEFT JOIN   user  u 
ON[tab][tab]  a[COLOR=#990000].[/color]userID [COLOR=#990000]=[/color] u[COLOR=#990000].[/color]ID 
LEFT [b][COLOR=#000000]JOIN[/color][/b]   [COLOR=#990000]([/color]
[tab][tab][tab]SELECT[tab]  articleID[COLOR=#990000],[/color] [b][COLOR=#000000]COUNT[/color][/b][COLOR=#990000]([/color]ID[COLOR=#990000])[/color] [b][COLOR=#0000FF]as[/color][/b] total_comment[COLOR=#990000],[/color] [b][COLOR=#000000]MAX[/color][/b][COLOR=#990000]([/color]timestamp[COLOR=#990000])[/color] [b][COLOR=#0000FF]as[/color][/b] lastcommentdate 
[tab][tab][tab]FROM[tab][tab]comment 
[tab][tab][tab]GROUP BY[tab]articleID
[tab][tab][tab][COLOR=#990000])[/color] AS c 
ON[tab][tab]  a[COLOR=#990000].[/color]ID [COLOR=#990000]=[/color] c[COLOR=#990000].[/color]articleID 
ORDER BY[tab]a[COLOR=#990000].[/color]ID DESC 
LIMIT[tab]   [COLOR=#993399]5[/color]
SQL[COLOR=#990000];[/color]
[tab]   
[tab][COLOR=#009900]$result[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]mysql_query[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$sql[/color][COLOR=#990000])[/color] [b][COLOR=#0000FF]or[/color][/b]
[tab][tab][b][COLOR=#0000FF]die[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]"SQL error: "[/color][COLOR=#990000].[/color][COLOR=#009900]$sql[/color][COLOR=#990000].[/color][COLOR=#FF0000]" ERROR: "[/color][COLOR=#990000].[/color][b][COLOR=#000000]mysql_error[/color][/b][COLOR=#990000]());[/color]
[tab][COLOR=#009900]$pattern[/color] [COLOR=#990000]=[/color] [COLOR=#990000]<<<[/color]HTML
[COLOR=#990000]<[/color]hr[COLOR=#990000]>[/color]
[COLOR=#990000]<[/color]h3[COLOR=#990000]>[/color]
[tab][COLOR=#990000]<[/color]a href[COLOR=#990000]=[/color][COLOR=#FF0000]"article()?id=%s\">%s</a>[/color]
[COLOR=#FF0000]</h3>[/color]
[COLOR=#FF0000]<b>Author %s, date %s</b>[/color]
[COLOR=#FF0000]<p>**%s**</p>[/color]
[COLOR=#FF0000]<b>%s</b> Comments and the last comment posted on %s[/color]
[COLOR=#FF0000]<br/> "[/color]
HTML[COLOR=#990000];[/color]   
[tab][b][COLOR=#0000FF]while[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$row[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]mysql_fetch_assoc[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$result[/color][COLOR=#990000])):[/color]
[tab][tab][b][COLOR=#000000]printf[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$pattern[/color][COLOR=#990000],[/color][tab][b][COLOR=#000000]urlencode[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$row[/color][COLOR=#990000][[/color][COLOR=#FF0000]'ID'[/color][COLOR=#990000]]),[/color] 
[tab][tab][tab][tab][b][COLOR=#000000]htmlspecialchars[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$row[/color][COLOR=#990000][[/color][COLOR=#FF0000]'title'[/color][COLOR=#990000]]),[/color] 
[tab][tab][tab][tab][b][COLOR=#000000]htmlspecialchars[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$row[/color][COLOR=#990000][[/color][COLOR=#FF0000]'username'[/color][COLOR=#990000]]),[/color][tab][tab][tab][tab][tab][tab]  
[tab][tab][tab][tab][tab][tab][tab][tab][b][COLOR=#000000]htmlspecialchars[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$row[/color][COLOR=#990000][[/color][COLOR=#FF0000]'timestamp'[/color][COLOR=#990000]]),[/color] 
[tab][tab][tab][tab][b][COLOR=#000000]htmlspecialchars[/color][/b][COLOR=#990000]([/color][b][COLOR=#000000]substr[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$row[/color][COLOR=#990000][[/color][COLOR=#FF0000]'content'[/color][COLOR=#990000]]),[/color][COLOR=#993399]0[/color][COLOR=#990000],[/color] [COLOR=#993399]100[/color][COLOR=#990000])),[/color] 
[tab][tab][tab][tab][b][COLOR=#000000]htmlspecialchars[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$row[/color][COLOR=#990000][[/color][COLOR=#FF0000]'total_comment'[/color][COLOR=#990000]]),[/color] 
[tab][tab][tab][tab][b][COLOR=#000000]htmlspecialchars[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$row[/color][COLOR=#990000][[/color][COLOR=#FF0000]'lastcommentdate'[/color][COLOR=#990000]])[/color]
[tab][tab][tab][tab][COLOR=#990000]);[/color]
[tab][b][COLOR=#0000FF]endwhile[/color][/b][COLOR=#990000];[/color]
[COLOR=#FF0000]}[/color]


public [b][COLOR=#0000FF]function[/color][/b] [b][COLOR=#000000]article[/color][/b][COLOR=#990000]()[/color] [COLOR=#FF0000]{[/color]
[tab][COLOR=#009900]$id[/color] [COLOR=#990000]=[/color] [b][COLOR=#0000FF]isset[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$_REQUEST[/color][COLOR=#990000][[/color][COLOR=#FF0000]'id'[/color][COLOR=#990000]])[/color] [COLOR=#990000]?[/color] [COLOR=#990000]([/color]int[COLOR=#990000])[/color] [COLOR=#009900]$_REQUEST[/color][COLOR=#990000][[/color][COLOR=#FF0000]'id'[/color][COLOR=#990000]][/color] [COLOR=#990000]:[/color] [COLOR=#993399]0[/color] [COLOR=#990000];[/color]
[tab][i][COLOR=#9A1900]// to display the comments related to the chosen article[/color][/i]
[tab][COLOR=#009900]$sql[/color] [COLOR=#990000]=[/color] [COLOR=#990000]<<<[/color]SQL
SELECT[tab]  a[COLOR=#990000].[/color]ID[COLOR=#990000],[/color] c[COLOR=#990000].[/color]content [b][COLOR=#0000FF]as[/color][/b] content[COLOR=#990000],[/color] c[COLOR=#990000].[/color]timestamp [b][COLOR=#0000FF]as[/color][/b] timestamp[COLOR=#990000],[/color] u[COLOR=#990000].[/color]username [b][COLOR=#0000FF]as[/color][/b] comment_author 
FROM[tab][tab]article AS a 
INNER JOIN  comment AS c 
ON[tab][tab]  a[COLOR=#990000].[/color]ID [COLOR=#990000]=[/color] c[COLOR=#990000].[/color]articleID 
INNER JOIN  user [b][COLOR=#0000FF]as[/color][/b] u 
ON[tab][tab]  u[COLOR=#990000].[/color]ID [COLOR=#990000]=[/color] c[COLOR=#990000].[/color]userID 
WHERE[tab]   a[COLOR=#990000].[/color]ID [COLOR=#990000]=[/color] [COLOR=#FF0000]"%s"[/color] 
ORDER BY[tab]c[COLOR=#990000].[/color]timestamp DESC 
LIMIT[tab]   [COLOR=#993399]5[/color]
SQL[COLOR=#990000];[/color]
[tab][COLOR=#009900]$query[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]sprintf[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$sql[/color][COLOR=#990000],[/color] [b][COLOR=#000000]mysql_real_escape_string[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$id[/color][COLOR=#990000]));[/color]
[tab][COLOR=#009900]$result[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]mysql_query[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$sql[/color][COLOR=#990000])[/color] [b][COLOR=#0000FF]or[/color][/b] 
[tab][tab][b][COLOR=#0000FF]die[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]"SQL error: "[/color][COLOR=#990000].[/color][COLOR=#009900]$sql[/color][COLOR=#990000].[/color][COLOR=#FF0000]" ERROR: "[/color][COLOR=#990000].[/color][b][COLOR=#000000]mysql_error[/color][/b][COLOR=#990000]());[/color]

[tab][COLOR=#009900]$pattern[/color] [COLOR=#990000]=[/color] [COLOR=#990000]<<<[/color]HTML
[COLOR=#990000]<[/color]hr[COLOR=#990000]>[/color]
[COLOR=#990000]<[/color]p[COLOR=#990000]>[/color][COLOR=#009900]%s[/color][COLOR=#990000]</[/color]p[COLOR=#990000]>[/color]
[COLOR=#990000]<[/color]b[COLOR=#990000]>[/color]Author [COLOR=#009900]%s[/color][COLOR=#990000],[/color] Date [COLOR=#009900]%s[/color][COLOR=#990000]</[/color]b[COLOR=#990000]>[/color]
[COLOR=#990000]<[/color]br[COLOR=#990000]/>[/color]
[COLOR=#990000]<[/color]br[COLOR=#990000]/>[/color]
HTML[COLOR=#990000];[/color]
[tab][b][COLOR=#0000FF]while[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$row[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]mysql_fetch_assoc[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$result[/color][COLOR=#990000])):[/color]
[tab][tab][b][COLOR=#000000]printf[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$pattern[/color][COLOR=#990000],[/color][tab][b][COLOR=#000000]htmlspecialchars[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$row[/color][COLOR=#990000][[/color][COLOR=#FF0000]'content'[/color][COLOR=#990000]]),[/color] 
[tab][tab][tab][tab][b][COLOR=#000000]htmlspecialchars[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$row[/color][COLOR=#990000][[/color][COLOR=#FF0000]'comment_author'[/color][COLOR=#990000]]),[/color] 
[tab][tab][tab][tab][b][COLOR=#000000]htmlspecialchars[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$row[/color][COLOR=#990000][[/color][COLOR=#FF0000]'timestamp'[/color][COLOR=#990000]])[/color]
[tab][tab][COLOR=#990000]);[/color]
[tab][COLOR=#FF0000]}[/color]
[COLOR=#FF0000]}[/color]
[COLOR=#990000]?>[/color]
 
Hi jpadie,

Code:
<h3>
    <a href="article()?id=%s\">%s</a>
</h3>

I take the above line from your code and change $id=%d for decimal like this:

<a href="article()?id=%d\">%s</a>

and put in my code and I still have this error:

Parse error: syntax error, unexpected 'article' (T_STRING) in C:\Database\DBAccess2.php on line 41

because DBAccess2.php contains the main() and article() above.

Thx to let me know the next step,


 
as you are using the value in a string, I suggest you use %s and not %d.

however there is a small error in this line
Code:
htmlspecialchars(substr($row['content']),0, 100)),

it should look like this instead
Code:
htmlspecialchars(substr($row['content'],0, 100)),
 

Hi,

Still have this error:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\Database\DBAccess.php on line 43

All codes are below:

Regards,

Code:
<?php

namespace Acme\WebAppBundle\Database;

class DBAccess {
    
    public function __construct() {
        mysql_connect('localhost', 'root','');
        mysql_select_db('blog');        
    }
  
public function main() {
    // SQL to display the article part with number of comments and last date time of comments
    $sql = <<<SQL
SELECT      a.ID as ID, a.title as title,a.content as content,a.timestamp as timestamp,u.username as username, 
            c.total_comment as total_comment, c.lastcommentdate as lastcommentdata 
FROM        article  a 
LEFT JOIN   user  u 
ON          a.userID = u.ID 
LEFT JOIN   (
            SELECT      articleID, COUNT(ID) as total_comment, MAX(timestamp) as lastcommentdate 
            FROM        comment 
            GROUP BY    articleID
            ) AS c 
ON          a.ID = c.articleID 
ORDER BY    a.ID DESC 
LIMIT       5
SQL;
       
    $result = mysql_query($sql) or
        die("SQL error: ".$sql." ERROR: ".mysql_error());
    $pattern = <<<HTML
"<hr>
<h3>
    <a href="article()?id=%s\">%s</a>
</h3>
<b>Author %s, date %s</b>
<p>**%s**</p>
<b>%s</b> Comments and the last comment posted on %s
<br/> "
HTML;   
    while ($row = mysql_fetch_assoc($result)):
        printf($pattern,    urlencode($row['ID']), 
                htmlspecialchars($row['title']), 
                htmlspecialchars($row['username']),                          
                htmlspecialchars($row['timestamp']), 
                htmlspecialchars(substr($row['content'],0, 100)), 
                htmlspecialchars($row['total_comment']), 
                htmlspecialchars($row['lastcommentdate'])
                );
    endwhile;
}


public function article() {
    $id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0 ;
    // to display the comments related to the chosen article
    $sql = <<<SQL
SELECT      a.ID, c.content as content, c.timestamp as timestamp, u.username as comment_author 
FROM        article AS a 
INNER JOIN  comment AS c 
ON          a.ID = c.articleID 
INNER JOIN  user as u 
ON          u.ID = c.userID 
WHERE       a.ID = "%s" 
ORDER BY    c.timestamp DESC 
LIMIT       5
SQL;
    $query = sprintf($sql, mysql_real_escape_string($id));
    $result = mysql_query($sql) or 
        die("SQL error: ".$sql." ERROR: ".mysql_error());

    $pattern = <<<HTML
<hr>
<p>%s</p>
<b>Author %s, Date %s</b>
<br/>
<br/>
HTML;
    while ($row = mysql_fetch_assoc($result)):
        printf($pattern,    htmlspecialchars($row['content']), 
                htmlspecialchars($row['comment_author']), 
                htmlspecialchars($row['timestamp'])
        );
    }
}
?>
 
i'd guess you have some whitespace after the heredoc. read the manual for the rules on heredoc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top