When I try to enter in an ID field to tag my individual posts (post-id:1932, etc.), the value always prints as 0, instead of the database’s value.
The query takes the $_REQUEST’s topic variable (generated from clicking a link).
Here is my code:
<?php
require_once 'connect.php';
$topic = $_REQUEST['topic'];
// Build the SELECT statement
$select_posts =
"SELECT p.topic, p.title, p.pic_id, p.snippet, p.content, p.author_id, p.date FROM posts p WHERE p.topic = '" . $topic . "' OR p.topic='Admin' ORDER BY p.date DESC";
// Run the Query
$result = mysql_query($select_posts);
?>
<?php
$post_count=0;
while ($post_count<10 && $posts = mysql_fetch_array($result)) {
$posts_row = sprintf(
'<entry>
<headline>%s</headline>
<pic><img class=inlinepic src=/Sustenance/assets/post-pics/%d.jpg></pic>
<snippet class=on id="%d snippet">%s</snippet>
<readmore id="readmore%d" class=on onclick=readmore("%d")>Read More</readmore>
<content id="%d" class="less">%s
<readmore class=on onclick=readless(%d)>Show Less</readmore>
</content>
<byline>
<by>by</by>
<author>%s</author>
</byline>
</entry>',
$posts['topic'], $posts['title'], $posts['pic_id'], $posts['post_id'], $posts['snippet'], $posts['post_id'], $posts['post_id'], $posts['post_id'], $posts['content'], $posts['post_id'], $posts['author_id']);
echo $posts_row;
$post_count++;
}
?>
What I find odd is the ‘pic_id’ prints properly and pulls in the correct picture, but even when I tried to reuse the pic_id in the $divid it doesn’t work. I’ve tried replacing the $divid with $posts[‘id’], using all the different integer type specifiers in the PHP Manual, and even using inline php.
Do I have a syntax error? Why does it keep printing out 0?
EDIT: I have posted the query as per requested
EDIT 2: Removed the $divid variable.
You do have to put the line
inside your
whileloop. The way it is now it tries to get a value of$posts['post_id']already before it is assigned for the first time in the loop head and therefore is0and never changes (since it’s never reassigned).