This will take a bit of explanation, so please bear with me...
The code below dynamically builds hyperlinks using two queries - query A and
query B. I want to optimize the code so I can omit query B and replace query A
with this (let's call it query C):
$link_cats = $wpdb->get_results("S ELECT wp_linkcategori es.cat_id,
wp_linkcategori es.cat_name, wp_links.link_c ategory FROM wp_linkcategori es INNER
JOIN wp_links ON wp_linkcategori es.cat_id = wp_links.link_c ategory WHERE
wp_links.link_c ategory 1);
The problem with the current code is that it unnecessarily executes query B with
every iteration of a loop (over the items generated by query A).
The solution, I think, is to find a way to reference (in the loop) the elements
returned by query C (rather than executing query B to get the required items).
Here's current code:
<?php
$link_cats = $wpdb->get_results("S ELECT cat_id, cat_name FROM
$wpdb->linkcategori es WHERE cat_id 1"); //query A
foreach ($link_cats as $link_cat)
{
$catid = $link_cat->cat_id; ?>
<h2>»&nbs p;<?php echo $link_cat->cat_name; ?></h2>
<?php $result = mysql_query("SE LECT link_url, link_descriptio n, link_name
FROM wp_links WHERE link_category = ".$catid); //query B
while ($linkdata = mysql_fetch_arr ay($result))
{
if ($pp)
{
$link = "<a href='".$linkda ta['link_url'];
}
elseif ($rg)
{
$link = "<a href='http://www.example.com/example/example.php";
}
if ($linkdata['link_descripti on'] == '0')
{
$linkdesc = "";
}
else
{
$linkdesc = "#cat".$linkdat a['link_descripti on'];
}
echo "• ". $link.$linkdesc ."'>".$linkd ata['link_name']."</a><br
/>";
}
}
?>
Here's pseudo code:
<?php
$link_cats = $wpdb->get_results("S ELECT wp_linkcategori es.cat_id,
wp_linkcategori es.cat_name, wp_links.link_c ategory FROM wp_linkcategori es INNER
JOIN wp_links ON wp_linkcategori es.cat_id = wp_links.link_c ategory WHERE
wp_links.link_c ategory 1); //query C
foreach ($link_cats as $link_cat)
{
$catid = $link_cat->cat_id; ?>
<h2>»&nbs p;<?php echo $link_cat->cat_name; ?></h2>
<?php $array_C = ??? how to get array of elements (previously retrieved by
query B)???
foreach (???what to loop over???)
{
if ($pp)
{
$link = "<a href='".$array_ C['link_url'];
}
elseif ($rg)
{
$link = "<a href='http://www.example.com/example/example.php";
}
if ($array_C['link_descripti on'] == '0')
{
$linkdesc = "";
}
else
{
$linkdesc = "#cat".$arr ay_C['link_descripti on'];
}
echo "• ". $link.$linkdesc ."'>".$array _C['link_name']."</a><br
/>";
}
}
?>
My question is this: How do I create an array of elements (array_C) that
contains only the items I need - that is, only the items returned by query B?
How do I iterate over that array to build the links?
Thanks in advance.
The code below dynamically builds hyperlinks using two queries - query A and
query B. I want to optimize the code so I can omit query B and replace query A
with this (let's call it query C):
$link_cats = $wpdb->get_results("S ELECT wp_linkcategori es.cat_id,
wp_linkcategori es.cat_name, wp_links.link_c ategory FROM wp_linkcategori es INNER
JOIN wp_links ON wp_linkcategori es.cat_id = wp_links.link_c ategory WHERE
wp_links.link_c ategory 1);
The problem with the current code is that it unnecessarily executes query B with
every iteration of a loop (over the items generated by query A).
The solution, I think, is to find a way to reference (in the loop) the elements
returned by query C (rather than executing query B to get the required items).
Here's current code:
<?php
$link_cats = $wpdb->get_results("S ELECT cat_id, cat_name FROM
$wpdb->linkcategori es WHERE cat_id 1"); //query A
foreach ($link_cats as $link_cat)
{
$catid = $link_cat->cat_id; ?>
<h2>»&nbs p;<?php echo $link_cat->cat_name; ?></h2>
<?php $result = mysql_query("SE LECT link_url, link_descriptio n, link_name
FROM wp_links WHERE link_category = ".$catid); //query B
while ($linkdata = mysql_fetch_arr ay($result))
{
if ($pp)
{
$link = "<a href='".$linkda ta['link_url'];
}
elseif ($rg)
{
$link = "<a href='http://www.example.com/example/example.php";
}
if ($linkdata['link_descripti on'] == '0')
{
$linkdesc = "";
}
else
{
$linkdesc = "#cat".$linkdat a['link_descripti on'];
}
echo "• ". $link.$linkdesc ."'>".$linkd ata['link_name']."</a><br
/>";
}
}
?>
Here's pseudo code:
<?php
$link_cats = $wpdb->get_results("S ELECT wp_linkcategori es.cat_id,
wp_linkcategori es.cat_name, wp_links.link_c ategory FROM wp_linkcategori es INNER
JOIN wp_links ON wp_linkcategori es.cat_id = wp_links.link_c ategory WHERE
wp_links.link_c ategory 1); //query C
foreach ($link_cats as $link_cat)
{
$catid = $link_cat->cat_id; ?>
<h2>»&nbs p;<?php echo $link_cat->cat_name; ?></h2>
<?php $array_C = ??? how to get array of elements (previously retrieved by
query B)???
foreach (???what to loop over???)
{
if ($pp)
{
$link = "<a href='".$array_ C['link_url'];
}
elseif ($rg)
{
$link = "<a href='http://www.example.com/example/example.php";
}
if ($array_C['link_descripti on'] == '0')
{
$linkdesc = "";
}
else
{
$linkdesc = "#cat".$arr ay_C['link_descripti on'];
}
echo "• ". $link.$linkdesc ."'>".$array _C['link_name']."</a><br
/>";
}
}
?>
My question is this: How do I create an array of elements (array_C) that
contains only the items I need - that is, only the items returned by query B?
How do I iterate over that array to build the links?
Thanks in advance.
Comment