...

View Full Version : simplifying a whole stack of codes in for loop



gilgalbiblewhee
07-29-2008, 10:54 PM
Is it possible in simplifying this stack of codes? They all have things in common but have differences as well:

$i=0;
if (!empty($keyword1))
{
echo "<span class='' id='' style='font-weight: bold; color: red;'> ".$keyword1."</span>\n";
$i++;
}
if (!empty($keyword2))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: blue;'> ".$keyword2."</span>\n";
}
if (!empty($keyword3))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: blue;'> ".$keyword3."</span>\n";
$i++;
}
if (!empty($keyword4))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: green;'> ".$keyword4."</span>\n";
$i++;
}
if (!empty($keyword5))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: orange;'> ".$keyword5."</span>\n";
$i++;
}
if (!empty($keyword6))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: purple;'> ".$keyword6."</span>\n";
$i++;
}
if (!empty($keyword7))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: aqua;'> ".$keyword7."</span>\n";
$i++;
}
if (!empty($keyword8))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: aqua;'> ".$keyword8."</span>\n";
$i++;
}
if ($_GET['book_spoke'] == 'Book_Spoke'){
{
if ($i > 0)
{
echo " AND ";
}
echo "<span class='' id='' style='font-weight: bold; color: black;'> in BOOK SPOKE ".$spoke."</span>\n";
$i++;
}
}
if ($_GET['chapter_spoke'] == 'Chapter_Spoke'){
{
if ($i > 0)
{
echo " AND ";
}
echo "<span class='' id='' style='font-weight: bold; color: black;'> in CHAPTER SPOKE ".$spoke."</span>\n";
$i++;
}
}
if ($_GET['verse_spoke'] == 'Verse_Spoke'){
{
if ($i > 0)
{
echo " AND ";
}
echo "<span class='' id='' style='font-weight: bold; color: black;'> in VERSE SPOKE ".$spoke."</span>\n";
$i++;
}
}
echo '<br />';

scoop_987
07-29-2008, 11:08 PM
This bit can be simplified into a for loop, But it means you have to set all the keywords into an array instead of individual arrays, otherwise its defeating the purpose.



if (!empty($keyword1))
{
echo "<span class='' id='' style='font-weight: bold; color: red;'> ".$keyword1."</span>\n";
$i++;
}
if (!empty($keyword2))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: blue;'> ".$keyword2."</span>\n";
}
if (!empty($keyword3))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: blue;'> ".$keyword3."</span>\n";
$i++;
}
if (!empty($keyword4))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: green;'> ".$keyword4."</span>\n";
$i++;
}
if (!empty($keyword5))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: orange;'> ".$keyword5."</span>\n";
$i++;
}
if (!empty($keyword6))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: purple;'> ".$keyword6."</span>\n";
$i++;
}
if (!empty($keyword7))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: aqua;'> ".$keyword7."</span>\n";
$i++;
}
if (!empty($keyword8))
{
if ($i > 0)
{
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: aqua;'> ".$keyword8."</span>\n";
$i++;
}


The looped Version:



//Instead of Doing some major changes, weve just make $keywords into an array
//by putting square brackets AROUND the number. Hence the reason that in
//the for loop, it starts at one rather than zero.

//Now we have to set the color array here.
$COLORS = array('red','blue','blue','green','orange','purple','aqua','aqua');
for($i = 1, $i < count($keyword); $i++){
if (!empty($keyword[$i])){
if ($i > 1) {
echo " + ";
}
echo "<span class='' id='' style='font-weight: bold; color: ".$COLORS[$i].";'> ".$keyword[$i]."</span>\n";
}
}

oesxyl
07-30-2008, 12:52 AM
function show_keyword($keyword, $color, $i){
if (!empty($keyword)){
if($i > 0){
echo ' + ';
}
echo '<span class="" id="" style="font-weight: bold; color: '.$color.';"> '.$keyword.'</span>\n';
$i++;
}
return $i;
}

$i=0;
$i = show_keyword($keyword1,'red',$i);
$i = show_keyword($keyword2,'blue',$i);
$i = show_keyword($keyword3,'blue',$i);
$i = show_keyword($keyword4,'green',$i);
$i = show_keyword($keyword5,'orange',$i);
$i = show_keyword($keyword6,'purple',$i);
$i = show_keyword($keyword7,'aqua',$i);
$i = show_keyword($keyword8,'aqua',$i);

if ($_GET['book_spoke'] == 'Book_Spoke'){
if ($i > 0){
echo " AND ";
}
echo "<span class='' id='' style='font-weight: bold; color: black;'> in BOOK SPOKE ".$spoke."</span>\n";
$i++;
}
if ($_GET['chapter_spoke'] == 'Chapter_Spoke'){
if ($i > 0){
echo " AND ";
}
echo "<span class='' id='' style='font-weight: bold; color: black;'> in CHAPTER SPOKE ".$spoke."</span>\n";
$i++;
}
if ($_GET['verse_spoke'] == 'Verse_Spoke'){
if ($i > 0){
echo " AND ";
}
echo "<span class='' id='' style='font-weight: bold; color: black;'> in VERSE SPOKE ".$spoke."</span>\n";
$i++;
}
echo '<br />';

scoop_987: sorry, I miss the part you already posted
gilgalbiblewhee: I take a break before continue with GET part, :) ( this way I discover scoop_987 post)
regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum