Jump to content

Getting first and last in loop per KEY


StefanRSA

Recommended Posts

I am trying to populate different select boxes from a DB lookup...

I look in two table.... One to the catid and then all subcats per catid...

I am not sure how this should work... My code as follow:

$catkey='1,4,5';
$query = "SELECT 
adcat.id AS catid, adsubcat.name AS subname, adsubcat.id AS subid, adsubcat.linkname AS sublink, 
adcat.clinkname AS clink, adcat.name AS catname
FROM adsubcat 
JOIN adcat ON adcat.id=adsubcat.catid
WHERE adsubcat.catid IN($catkey) ORDER BY adsubcat.name ASC";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)){
$catid=$row['catid'];
$subid=$row['subid'];
$subname=$row['subname'];
////////////////////////////////////////////////////////////////////////////// STUFF I AM NOT SURE OF STARTS HERE
// The loop should now echo at the start of every echo $catname.'<select id="subcat" multiple="multiple" name="sub[]"><option value="" >ALL SUB-CATEGORIES</option>';
////And for every subcat per id it should echo '<option value="'.$subid.'">'.$subname.'</option>';
///// The loop per Catid should end with echo '<select>';
////////////////////////////////////////////////////////////////////////////// STUFF I AM NOT SURE OF ENDS HERE
}

 

I want it then to print out as follow:

CAT 1

<select id="catid1" multiple="multiple" name="cat1[]">

<option value="0" >ALL SUB-CATEGORIES FOR Cat 1</option>

<option value="1" >Subname 1.1</option>

<option value="2" >Subname 1.2</option>

</select>

 

CAT 4

<select id="catid4" multiple="multiple" name="cat4[]">

<option value="0" >ALL SUB-CATEGORIES FOR Cat 4</option>

<option value="4" >Subname 2.4</option>

<option value="7" >Subname 2.7</option>

</select>

 

CAT 5

<select id="catid5" multiple="multiple" name="cat5[]">

<option value="0" >ALL SUB-CATEGORIES FOR Cat 5</option>

<option value="65" >Subname 5.65</option>

<option value="34" >Subname 5.34</option>

</select>

Link to comment
https://forums.phpfreaks.com/topic/214815-getting-first-and-last-in-loop-per-key/
Share on other sites

Fixed it the long way around:

$catquery="SELECT * FROM adcat WHERE id IN($catkey) ORDER BY name ASC";
$catresult = mysql_query($catquery) or die(mysql_error());
while($crows = mysql_fetch_array($catresult)){
$thisid=$crows['id'];
$catname=$crows['name'];
echo '<p>'.$catname.'<br>
<select style="display: none;" id="CAT'.$thisid.'" multiple="multiple" name="sub'.$thisid.'[]">
<option value="0" >ALL SUB-CATEGORIES</option>'; ///////////////// ECHO SELECTBOX START HERE
$query = "SELECT 
adcat.id AS catid, adsubcat.name AS subname, adsubcat.id AS subid, adsubcat.linkname AS sublink, 
adcat.clinkname AS clink, adcat.name AS catname
FROM adsubcat 
JOIN adcat ON adcat.id=adsubcat.catid
WHERE catid='$thisid' ORDER BY adsubcat.name ASC";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$catid=$row['catid'];
$subid=$row['subid'];
$subname=$row['subname'];
echo '<option value="'.$subid.'">'.$subname.'</option>';
}
echo '</select></p><div style="height:10px;width:100%:clear:both;"></div>';///////////////// ECHO SELECTBOX END HERE
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.