I’m having trouble with an array that pulls a list of id’s from one database and then does a search of another database for each id.
This looks a little sloppy to me, but I’m not sure how to fix it.
Here’s what I’m trying to do:
I open database=‘athensga’ table=‘followers’ and pull a list of memberid’s.
Then, for each of the memberid’s I select I create an array:
select each row with the matching memberid from database=‘athensgacalendar’ table='calendar_events
Currently I’m not getting any errors or anything, just blank space.
Here’s the code:
<?php
// Display Favorites List
include ‘…/includes/link_athensga.php’;
$sql = “SELECT memberid FROM followers WHERE follower=‘$uid’ LIMIT 14”;
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = ‘Error getting list of favorites.’;
include ‘…/includes/error.html.php’;
exit();
}
// For each Favorite
while ($row = mysqli_fetch_array($result))
{
$posts = array(‘memberid’ => $row[‘memberid’]);
}
// Display Posts From My Favorite Organizations
foreach ($posts as $post):
include ‘link_athensgacalendar.php’;
$result = mysqli_query($link, “SELECT id, event_title, event_shortdesc, event_description, event_start, orgname, type FROM calendar_events WHERE memberid=$post[‘memberid’] AND month(NOW()) = month(event_start) AND dayofmonth(NOW()) = dayofmonth(event_start) AND year(NOW()) = year(event_start) LIMIT 11”);
if (!$result)
{
$error = ‘Error fetching events from database!’;
include ‘error.html.php’;
exit();
}
Your code could go wrong anywhere.
I’m guessing your host has display_errors set to Off in php.ini, so you don’t see any php errors.
Could you try and put something like
echo "Hi";
at several places in your code and see which are output and which not. At the point the output stops is an error.
BTW, you can use [ PHP ] and [ /PHP ] to show PHP in your post
(remove the spaces between [, PHP, and ], I put those spaces there so the forum will show the plain text). Your code will then look like
<?php
// Display Favorites List
include '../includes/link_athensga.php';
$sql = "SELECT memberid FROM followers WHERE follower='$uid' LIMIT 14";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error getting list of favorites.';
include '../includes/error.html.php';
exit();
}
// For each Favorite
while ($row = mysqli_fetch_array($result))
{
$posts[] = array('memberid' => $row['memberid']);
}
// Display Posts From My Favorite Organizations
foreach ($posts as $post):
include 'link_athensgacalendar.php';
$result = mysqli_query($link, "SELECT id, event_title, event_shortdesc, event_description, event_start, orgname, type FROM calendar_events WHERE memberid=$post['memberid'] AND month(NOW()) = month(event_start) AND dayofmonth(NOW()) = dayofmonth(event_start) AND year(NOW()) = year(event_start) LIMIT 11");
if (!$result)
{
$error = 'Error fetching events from database!';
include 'error.html.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$orgposts[] = array('id' => $row['id'], 'event_title' => $row['event_title'], 'orgname' => $row['orgname'], 'event_shortdesc' => $row['event_shortdesc']);
}
if (count($orgposts) > 0)
{
foreach ($orgposts as $orgpost): ?>
<form action="" method="post">
<li style="line-height: 19px; padding-left: 5px; text-indent: -5px; " class="full-width">
<a href="?action=Details&id=<?php echo $orgpost['id']; ?>">
<p style="text-indent: -5px; " class="paragraph_style_1">
<span style="font-size: 0px; position: relative; top: -8px; " class="Bullet"></span>
<span style="width: 5px; " class="inline-block"></span><?php htmlout($orgpost['event_title']); ?> - <?php htmlout($orgpost['event_shortdesc']); ?>
</p>
</a>
</li>
<li style="line-height: 14px; padding-left: 5px; text-indent: -5px; " class="full-width">
<p style="text-indent: -5px; " class="paragraph_style_2">
<span style="clear: left; float: left; font-size: 0px; position: relative; top: -6px; " class="Bullet"></span>
<span style="width: 5px; " class="inline-block"></span>Posted by <?php htmlout($orgpost['orgname']); ?>
</p>
</li>
<?php endforeach; }
else { ?>
<p style="text-indent: -5px; " class="paragraph_style_1">No events have been posted today by your favorite organizations.
<?php }
endforeach;
?>
Much nicer, right?
PS. I also added some indenting and corrected some incorrect HTML.
I also removed the “}” at the last line since I couldn’t match it with any open “{”. Does that solve your error ?
Ok, so I’ve figured out that my problem with with the second query.
I’m able to echo variables from the first query, but the second isn’t returning anything.
Could it be because I’m trying to query two different databases at once? I’ve never tried to do this before.
Here’s the updated code:
// Display Favorites List
include '../includes/link_athensga.php';
$sql = "SELECT memberid FROM followers WHERE follower='$uid'";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error getting list of favorites.';
include '../includes/error.html.php';
exit();
}
// For each Favorite
while ($row = mysqli_fetch_array($result))
{
$posts[] = array('memberid' => $row['memberid']);
}
// Display Posts From My Favorite Organizations
foreach ($posts as $post):
$favorg = ($post['memberid']);
include '../includes/link_athensgacalendar.php';
$result = mysqli_query($link, "SELECT id, event_title, event_shortdesc, event_description, event_start, orgname, type FROM calendar_events WHERE memberid='$favorg' AND month(NOW()) = month(event_start) AND dayofmonth(NOW()) = dayofmonth(event_start) AND year(NOW()) = year(event_start) LIMIT 11");
if (!$result)
{
$error = 'Error fetching events from database!';
include 'error.html.php';
exit();
}