0

While dynamically generating a page with different types of contents , the "post" content is appearing above the static content which is being generated.I want it the other way around. Does there appear to be anything in my code that would make this happen, or do you think the problem has something to do with my database? Thanks.

$query = "SELECT * FROM content WHERE pages LIKE '%$pageID%'";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {

// Display pages's static content

if ($row['type'] == "static") {

    echo "
        <h2>" . $row['tile'] . "</h2>
        <content>" . $row['body'] . "</content>
    ";
}

// Display pages's posts

else {
    echo "
        <h2>" . $row['tile'] . "</h2>
        <content>" . $row['body'] . "</content>
    ";
}
1
  • Add a separator between types.Check if your $row['type'] is a correct variable Commented Jul 21, 2012 at 21:33

2 Answers 2

1
SELECT * FROM content WHERE pages LIKE '%$pageID%' ORDER BY type desc
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Nice and easy. Thanks.
0

Add this to the end of your query:

ORDER BY CASE WHEN type = 'static' THEN 0 ELSE 1 END

2 Comments

THANK YOU, that did the trick! I wasted hours stressing over this one. Any chance you could explain to me what that code does exactly? Looks like I need to learn MySQL better.
Sure. ORDER BY [somecolumn] will order the results by the column you pass. Instead of passing a column, I'm passing a conditional expression (CASE WHEN ... THEN ... END), to force rows where the type equals 'static' to be returned first (0).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.