I'm fairly new to using PHP functions, up until now my code has largely consisted of holding a bunch of queries in a file (queries.php) and call them into my main pages using foreach
loops.
Now I'm building a small network site that has a lot of dynamic data to deal with.
I've heard that functions are the way to go for this kind of project and although I have tried to convert some of my queries into functions and my for each loops into function calls I haven't been successful - often receiving parse errors.
I do pretty well by being shown an example and adapting it to my needs but have struggled with what tutorials I have been able to find on the net. I have included some sample code in the hope that someone can show me and I can go from there.
I guess I am asking to be guided through how I would convert my current query into a function and calling the function instead of using a foreach
loop or having to call the same query multiple times?
An example of a function;
function getProfileGridsWithLocations(){
// BEGIN QUERY TO DISPLAY MEMBERS WITH LOCATION ON HOMEPAGE
$query = "SELECT
users.id AS id,
users.username AS username,
users.email as email,
users.created,
profilephotos.profilephoto_file AS photo,
profiles.profile_displayname AS displayname,
profiles.profile_displayage AS displayage,
locations.user_id,
locations.location_city,
frmlocations.location_id,
frmlocations.city AS City
FROM users
JOIN profilephotos ON users.id = profilephotos.user_id
JOIN profiles ON users.id = profiles.user_id
JOIN locations ON users.id = locations.user_id
JOIN frmlocations ON locations.location_city = frmlocations.location_id
WHERE locations.location_city = '$location' OR '$location' = ''
ORDER BY users.created DESC
/*LIMIT 20*/
";
try
{
$stmt = $db->prepare($query);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$members_with_locations_homepage = $stmt->fetchAll();
}
An example of my foreach loop;
<div id="profiles" class="pages">
<div class="mem-grid-wrap">
<?php foreach($this->getProfileGridsWithLocations as $members_with_location_homepage): ?>
<div class="box">
<div class="boxInner" style="background-color:#000000;">
<a href="girl.php?username=<?php echo htmlentities($members_with_location_homepage['username'], ENT_QUOTES, 'UTF-8'); ?>&uid=<?php echo htmlentities($members_with_location_homepage['id'], ENT_QUOTES, 'UTF-8'); ?>">
<?php
echo (!empty($members_with_location_homepage['photo'])) ? "<img src='uploads/profile-photos/" . $members_with_location_homepage['photo']. "' />" : "<img src='assets/img/image-not-available.jpg' />";
?>
<div class="titleBox" style="text-align:left; line-height:20px;">
<span style="font-size:18px; font-weight:bold;"><i class="fa fa-female"></i> <?php echo htmlentities($members_with_location_homepage['displayname'], ENT_QUOTES, 'UTF-8'); ?>, <?php echo htmlentities($members_with_location_homepage['displayage'], ENT_QUOTES, 'UTF-8'); ?></span>
<br />
<span style="font-size:11px;"><i class="fa fa-map-marker"></i> <?php echo htmlentities($members_with_location_homepage['City'], ENT_QUOTES, 'UTF-8'); ?> | <i class="fa fa-clock-o"></i> Now</span>
</div>
</a>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
I've not included the query that I'm currently using as I have used that in the function, provided above.