Jim R Posted September 21, 2017 Share Posted September 21, 2017 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. Link to comment Share on other sites More sharing options...
requinix Posted September 21, 2017 Share Posted September 21, 2017 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? Link to comment Share on other sites More sharing options...
Jim R Posted September 21, 2017 Author Share Posted September 21, 2017 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. Link to comment Share on other sites More sharing options...
requinix Posted September 21, 2017 Share Posted September 21, 2017 Not only is it possible, it's downright likely. Adjust the delimiter to explode(). Link to comment Share on other sites More sharing options...
Jim R Posted September 21, 2017 Author Share Posted September 21, 2017 I had that in there, but it was just the comma. I added a space after the comma...it worked. Thank you. Link to comment Share on other sites More sharing options...
Barand Posted September 21, 2017 Share Posted September 21, 2017 ...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. Link to comment Share on other sites More sharing options...
ginerjm Posted September 21, 2017 Share Posted September 21, 2017 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. Link to comment Share on other sites More sharing options...
Barand Posted September 21, 2017 Share Posted September 21, 2017 That would cure the problem of two spaces but not that of a missing space or mis-spellings. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.