I am using the following code poor form. I am interested in how I can optimise this and use the correct methods. I understand the DRY concept and will look to employ that throughout my code. However, this works and its pretty quick too, even with large result sets. I would like to learn and improve this code.
<?php
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
$blogUrl = get_site_url();
global $wpdb;
$DBquery = "
SELECT *
FROM saved_collection
WHERE userid = '".$user_id."'
";
$result = $wpdb->get_results($DBquery);
$ids = array();
foreach($result as $row)
{
$collectionID = $row->id;
$collection_name = $row->collection_name;
$collectionURL = '<a href ="' . $blogUrl . '/client/?gid=' .$user_id. '&c=' .$collectionID. '"target="_blank">Programme Link</a>';
echo '<div class="well">';
echo '<h1>' . $collection_name . '</h1>';
echo $collectionURL;
echo '<div class="clearfix"></div>';
$cleandata = unserialize($row->collection_data);
foreach($cleandata as $data)
{
$ids[] = $data['ExerciseID'];
// START OF QUERY TO GET THE FIRST IMAGE FROM EACH EXERCISE
global $wpdb;
$query = "
SELECT *
FROM imagemap
WHERE exercise = '".$data['ExerciseID']."'
LIMIT 1
";
$result = $wpdb->get_results($query);
foreach($result as $row)
{
echo '<div class="imageGridImages imageGridImagesLarge">';
// START OF QUERY TO GET NAME OF EACH IMAGE
global $wpdb;
$exerciseTitleQuery = "
SELECT post_title
FROM wp_posts
WHERE id = '".$data['ExerciseID']."'
LIMIT 1
";
$exerciseTitleQueryResult = $wpdb->get_results($exerciseTitleQuery);
foreach ($exerciseTitleQueryResult as $returned_post_title)
{echo '<div class="thumbnail-title-block">';
echo '<p>' .$returned_post_title->post_title. '</p>';
echo '</div>';
}
// END OF QUERY TO GET NAME OF EACH IMAGE
echo "<img src=".$blogUrl. "/image/medium/".$row->id. ".jpg />";
//echo '<div class="image display" style="background-image: url(' .$blogUrl.'/image/thumb/' .$row->id. '.jpg);"></div>';
echo '</div>';
// end of div imageGridImages
}
}
echo '<div class="clearfix"></div>';
echo '</div>';
}
?>
foreach($result as $row)
. This introduces confusion as to what isresult
and what isrow
\$\endgroup\$