post 2 not listed? I want it to be on the list in post 2 main-post disappears while post titles loop! I couldn't figure out why and how. can you help me............................. ......
<?php
/* ********************************* */
// GETTING POST ID
/* ********************************* */
$icinde_id = NULL;
if (!empty($_GET['icinde_id'])) {
$icinde_id = intval($_GET['icinde_id']); // Making sure that we received an integer ID
} else {
echo "A post ID must be provided!";
die;
}
/* ********************************* */
// FETCHING THE POST
/* ********************************* */
$main_post = mysqli_query(
$db_connection,
"SELECT * FROM icindekiler WHERE icinde_id=$icinde_id"
);
$main_post_data = NULL; // Will become the associative array
if (mysqli_num_rows($main_post) === 1) {
$main_post_data = mysqli_fetch_assoc($main_post);
} else {
echo "Post not found";
die;
}
/* ********************************* */
// FETCHING RELATED POSTS
/* ********************************* */
$icinde_kitap_id = $main_post_data['icinde_kitap_id'];
$related_posts_query = "
SELECT
*
FROM
icindekiler
WHERE
icinde_kitap_id = $icinde_kitap_id
AND
icinde_id != $icinde_id
";
$related_posts = mysqli_query(
$db_connection,
$related_posts_query
);
$related_posts_data = [];
if (mysqli_num_rows($related_posts) > 0) {
$related_posts_data = mysqli_fetch_all($related_posts, MYSQLI_ASSOC);
}
/* ********************************* */
// THE END
/* ********************************* */
//print_r($main_post_data);
//echo "<pre>";
//print_r($related_posts_data);
?>
This is how the for loop works...
<?php foreach ($related_posts as $row) {?>
<?php echo $row["icinde_baslik"] ;?>
<?php } ?>
But I couldn't get the main-post title to be in the list.
What should be done to achieve this?
CodePudding user response:
You are explicitly excluding the main post from the $related_posts
data - as per the AND icinde_id != $icinde_id
in the WHERE clause of your second query.
Unless you really need to keep the "related posts" data separate for some other purpose in another part of your script, you could get it to just output the main post in the same dataset.
I can't see your database obviously, but I think
SELECT * FROM
icindekiler
WHERE
icinde_kitap_id = $icinde_kitap_id
OR icinde_id = $icinde_id
would work for that
** Obviously please adjust the above to prevent SQL injection and related problems by parameterising the input variables.