Jump to content

[SOLVED] integrating a function within HTML


Foser

Recommended Posts

This is my function: (works perfecly)

function news(){
$query = mysql_query("SELECT `post_subject`, post_text FROM `phpbb_posts` WHERE `forum_id` = 2 ORDER by `post_id` DESC LIMIT 2");

while ($fetch = mysql_fetch_assoc($query)) {
$article[] = $fetch;
}
$article[0]['post_subject']; 
$article[1]['post_subject']; 
$article[0]['post_text'];
$artcile[1]['post_text'];
}

 

Now it's not showing up in my html.

 

here what've done. I called news(); at  the beginning and I put the variables where I needed to into my html such as:

<h2><?php echo $article[0]['post_subject']; ?></h2>
		<?php echo smiley_text($article[0]['post_subject']); ?><br /><br />
		<a href="#">Message Footer information</a>
	</div>

 

No text is showing up when I execute it. Whats a similar way to do this correctly?

 

thanks

 

Are you sure your information is coming out of the database correctly? Your syntax looks like it has some problems with it. Try this:

 

$query = mysql_query("SELECT post_subject, post_text FROM phpbb_posts WHERE forum_id='2' ORDER by post_id DESC LIMIT 2");

It very well may execute it, but you need to return the array from the function, then use it where you like. eg;

 

<?php $article = news(); ?>
<h2><?php echo $article[0]['post_subject']; ?></h2>
<?php echo smiley_text($article[0]['post_subject']); ?><br /><br />
<a href="#">Message Footer information</a>
</div>

This is the error I get:

Fatal error: Cannot use string offset as an array in C:\WAMP\www\CodersDesk\index.php on line 47

 

not sure if this is exacly how you do the returns... but...

 

return $article[0]['post_subject']; 
return $article[1]['post_subject']; 
return $article[0]['post_text'];
return $artcile[1]['post_text'];

 

 

You can only return one thing from a function.

 

<?php

function news(){
  $query = mysql_query("SELECT `post_subject`, post_text FROM `phpbb_posts` WHERE `forum_id` = 2 ORDER by `post_id` DESC LIMIT 2");

  while ($fetch = mysql_fetch_assoc($query)) {
    $article[] = $fetch;
  }
  return $article;
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.