Help - Search - Members - Calendar
Full Version: HELP! Developer quit! Question on "foreach"
HTMLHelp Forums > Programming > Server-side Scripting
albertg
My developer quit (by my choice wacko.gif ) and I now have to clean up some code. I'm not an expert at cleaning up others code so I need your help. The following code creates multiple select boxes based on the "foreach ($skillChapters as $skillChapter)", one for each database record. I need to remove the multiple boxes option and replace with just the first database record in the table "skillchapters" with record "1". I can remove the "foreach ($skillChapters as $skillChapter)" and it only shows one box but I don't know how to specify the database record I need to show in the box. Here is the code:


echo $form->create( 'Job', array( 'action' => 'index' ) )."\n";
echo "<div class=\"row\"><label>Skills</label>";
foreach ($skillChapters as $skillChapter)
{
$currid = $skillChapter['SkillChapter']['id'];
echo "<div class=\"selectSkill\">";
echo "<div class=\"floatLeft\" onclick=\"showHide('showchapter".$currid."', 'showRightMinus".$currid."', 'showRightPlus".$currid."')\">".$skillChapter['SkillChapter']['title']."</div>";
echo "<div class=\"floatRight\" onclick=\"showHide('showchapter".$currid."', 'showRightMinus".$currid."', 'showRightPlus".$currid."')\">";
echo "<span class=\"showElement\" id=\"showRightPlus".$currid."\">+</span><span class=\"hideElement\" id=\"showRightMinus".$currid."\">(close)</span>";
echo "</div>";

$skillCategories = $skillChapter['SkillCategory'];
echo "<div class=\"hideSkillBox\" id=\"showchapter".$currid."\">";
$iteration =0;
$skilIds = array();
foreach ($skillCategories as $idx=>$skillCategory)
{

echo (($idx !=0) && ($idx%3 ==0) ) ? '<hr>' : '';
// $clear = (($idx ==0) || ($idx%3 ==0) ) ? 'floatClear' : '';
// echo "<div class=\"skillset $clear\">";
echo "<div class=\"skillset\">";

echo "<div class=\"skillCategory\">";
echo $skillCategory['title'];
echo "</div>";
$skills = $skillCategory['Skill'];
foreach ($skills as $skill)
{
$skillid = $skill['id'];
$skillIds[] = array("skill".$skill['id'],$skill['title'],$skillChapter['SkillChapter']['title']);

echo "<div class=\"skillRow\">";
echo "<input type=\"checkbox\" name=\"data[Job][skill".$skillid."]\" value=\"1\" id=\"skill".$skillid."\"> ";
echo "<label for=\"skill".$skillid."\">{$skill['title']}</label>";
echo "</div>";
}
echo "</div>";

$iteration++;
}
echo "</div>";
echo "</div>";
Brian Chandler
foreach ($things as $thing)
{
<block>
}

means "Do <block> lots of times, with $thing taking all of the values in the array $things in turn.

So if you want to do <block> once, first set $thing to the single thing you want to do it for, and thus write:

$thing = <whatever>
<block>


You haven't shown us where the $things are retrieved from the database, so I can't guess how to do it. What DB are you using anyway?
albertg
its retrieving data from Mysql database. Can i just replace the foreach clause with a fixed value?
Brian Chandler
Please read my answer again for your second question. ("foreach" isn't a 'clause' in any useful sense. ... what I wrote) You do understand what { ... } means, right?


The vulgar and disgusting thing to do is just to write

break;

inside the end of the foreach loop, so it loops once then stops.

The proper thing to do is to see where the array skillChapters is coming from, and selec the one you want, or change the mysql_query() call to extract a single record only. You may find this fragment helpful:

echo "<p><pre>"; print_r($skillChapters); echo "</pre>";

This will dump the contents of the array so you can see what's going on.

HTH
albertg
Dude... your suggestions worked! I just may have to drop you a couple bucks! Do you by chance have any suggestions on a CSS hack to make a scroll drop down work in IE 6? I'm pulling my hair out for days. Nothing I have tried has worked. Works in IE 7 and FF3. Here is the CSS:

.showSkillBox {
max-height:300px;
display:default;
border:1px solid #A7A6AA;
position: absolute;
background: #fff;
margin:17px 0 0 -4px;
width:400px;
background-color: #fff;
overflow: auto;
}
Christian J
QUOTE(albertg @ Oct 6 2008, 09:27 PM) *

.showSkillBox {
max-height:300px;
display:default;
border:1px solid #A7A6AA;
position: absolute;
background: #fff;
margin:17px 0 0 -4px;
width:400px;
background-color: #fff;
overflow: auto;
}

IE6 doesn't support "max-height", try "height" instead. Also there's no value "default" for the "display" property, and there's a duplicate "background" property.
albertg
Perfect. Thanks so much!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.