Jump to content

Exploding data and putting it through an IF loop...


Go to solution Solved by requinix,

Recommended Posts

I have a list of colleges, and I've exploded the data to create a list.  The player will inevitably commit ($line['commit']) to a college, which will be reflected in a few places, but I also want an * next to that college in the list. 

 

(I've already echoed $line['commit'] just above the code below to note it's getting it properly.)

// Turn CSV into unordered list
					$college = $line['list'];
					$college = explode(",",$college);
					asort($college);
					foreach ($college as $collegeList) {
					echo '<tr><td>' . $collegeList;
						if ($collegeList == $line['commit']) {
						 echo '*';
						}
					echo '</td></tr>';	

if ($collegeList == $line['commit']) {

                         echo '*';

 

...that doesn't echo any *.  

 

 

if ($collegeList = $line['commit']) {

                         echo '*';

 

...puts an * by each item on the list. 

 

 

One = is for assignment, two ==s is for comparison. You want a comparison.

 

If the star isn't showing up then that means one or both of the values that you think should be equal, aren't.

 

Working with a CSV? How did you load the lines of the file? Is either the "list" or "commit" values the last one on the line?

  • Like 1

Commit is just one value:  

 

Michigan State

 

List is:  

 

Butler, Xavier, Illinois, Michigan State, Ohio State

 

 

(written as is, no other punctuation)

 

Is it possible it's reading Michigan State with a space ahead of it?  

 

 

EDIT:  That's what it was!  It was comparing Michigan State to _Michigan State.  

 

These are the bittersweet moments of being a novice coder.   :stoopid:  :happy-04:

Edited by Jim R

...it worked

 

Until someone creates a list with a comma and two spaces, or forgets the space, or mis-spells a university name, ...

 

Relying on text comparisons is frought with problems, as is storing data in delimited lists.

IOW - Barand is suggesting you add the following:

 

 

if (trim($collegeList) == $line['commit']) {

 

To eliminate those nasty Extra spaces.  You can also remove other unwanted chars if you know what they are.  Check the manual.

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.