Jump to content

Recommended Posts

I have a longl list of if statements and the only thing that is different is the variable name.  The variable name itself is the same but just a number at the end of the variable changes

 

experiece0, experience1, experience2.....experience9

 

This is the current code

	if(!empty($experience1)) {
	$experience1 = explode(",",$experience1);
	echo "<tr><td> </td>";
	foreach($experience1 as $exp1) {
		echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp1))."</td>";
	}
	echo "</tr>";
}
if(!empty($experience2)) {	
	$experience2 = explode(",",$experience2);
	echo "<tr><td> </td>";
	foreach($experience2 as $exp2) {
		echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp2))."</td>";
	}
	echo "</tr>";
}
        ...
        ...
        ...
        ...
if(!empty($experience3)) {
	$experience3 = explode(",",$experience3);
	echo "<tr><td> </td>";
	foreach($experience3 as $exp3) {
		echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp3))."</td>";
	}
	echo "</tr>";
}

 

i want to make it more efficient by using a for loop and incrementing the variable number like this but it doesn't work

for($i=1;$i<9;$i++) {
if(!empty($experience.$i)) {
	$experience.$i = explode(",",$experience.$i);
	echo "<tr><td>&nbs;</td>";
	foreach($experience.$i as $exp.$i) {
		echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp.$i))."</td>";
	}
echo "</tr>";
}
}

i get this error message

Parse error: parse error, expecting `')'' in C:\Apache\htdocs\folk\view-volunteer.php  on line 119

How are your variable names being populated? Manually or from GET/POST, if from GET/POST that means you either extracted the array of have register_globals on, both of which are not good practice. That and you can easily manipulate / use them in an array better then you can as a variable. Incase that is the case here is an example:

 

for($i=1;$i<9;$i++) {
    if(!empty($_POST['experience' . $i])) {
        $experience = explode(",",$_POST['experience' . $i);
        echo "<tr><td>&nbs;</td>";

        foreach($experience as $exp) {
            echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp))."</td>";
        }

        echo "</tr>";
    }
}

 

If register_globals are not on and the original data is not in array but is defined this would work:

for($i=1;$i<9;$i++) {
    $experience = ${$experience . $i};
    if(!empty($experience) {
        $experience = explode(",",$experience);
        echo "<tr><td>&nbs;</td>";

        foreach($experience as $exp) {
            echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp))."</td>";
        }

        echo "</tr>";
    }
}

 

Both untested, but hopefully gives you a working theory.

for($i=1;$i<9;$i++) {
    $experience = ${$experience . $i};
    if(!empty($experience) {
        $experience = explode(",",$experience);
        echo "<tr><td>&nbs;</td>";

        foreach($experience as $exp) {
            echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp))."</td>";
        }

        echo "</tr>";
    }
}

 

Didn't work

 

I tried something similar but it displays nothing

I have 10 fields in the database

 

Given that you have 10 fields in the database you can get this data out via an array.  Somewhat psuedo code here is how it would work:

 

while ($row = mysql_fetch_assoc($result)) {
  for($i=1;$i<9;$i++) {
    $experience = $row['experience' . $i];
    if(!empty($experience) {
        $experience = explode(",",$experience);
        echo "<tr><td>&nbs;</td>";

        foreach($experience as $exp) {
            echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp))."</td>";
        }

        echo "</tr>";
    }
  }
}

 

And it should work, given that you have valid queries and are fetching the data properly. Without seeing the full script, I just have to make guesses so yea.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.