Foser Posted February 3, 2008 Share Posted February 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/89198-solved-integrating-a-function-within-html/ Share on other sites More sharing options...
corillo181 Posted February 3, 2008 Share Posted February 3, 2008 how are you getting the result out of the function because i don't see any return type or echo. Quote Link to comment https://forums.phpfreaks.com/topic/89198-solved-integrating-a-function-within-html/#findComment-456724 Share on other sites More sharing options...
Foser Posted February 3, 2008 Author Share Posted February 3, 2008 Well it's because all the finishing last 4 variables will be in different places, echo or return wound't work for me here. That is why I am asking if there is another way to do it like this. Quote Link to comment https://forums.phpfreaks.com/topic/89198-solved-integrating-a-function-within-html/#findComment-456728 Share on other sites More sharing options...
haku Posted February 3, 2008 Share Posted February 3, 2008 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"); Quote Link to comment https://forums.phpfreaks.com/topic/89198-solved-integrating-a-function-within-html/#findComment-456732 Share on other sites More sharing options...
Foser Posted February 3, 2008 Author Share Posted February 3, 2008 I changed it to what you have done, and still does not execute it. Quote Link to comment https://forums.phpfreaks.com/topic/89198-solved-integrating-a-function-within-html/#findComment-456738 Share on other sites More sharing options...
trq Posted February 3, 2008 Share Posted February 3, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/89198-solved-integrating-a-function-within-html/#findComment-456747 Share on other sites More sharing options...
Foser Posted February 3, 2008 Author Share Posted February 3, 2008 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']; Quote Link to comment https://forums.phpfreaks.com/topic/89198-solved-integrating-a-function-within-html/#findComment-456757 Share on other sites More sharing options...
trq Posted February 3, 2008 Share Posted February 3, 2008 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89198-solved-integrating-a-function-within-html/#findComment-456765 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.