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. Quote Link to comment https://forums.phpfreaks.com/topic/305064-exploding-data-and-putting-it-through-an-if-loop/ 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? 1 Quote Link to comment https://forums.phpfreaks.com/topic/305064-exploding-data-and-putting-it-through-an-if-loop/#findComment-1551689 Share on other sites More sharing options...
Jim R Posted September 21, 2017 Author Share Posted September 21, 2017 (edited) 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. Edited September 21, 2017 by Jim R Quote Link to comment https://forums.phpfreaks.com/topic/305064-exploding-data-and-putting-it-through-an-if-loop/#findComment-1551698 Share on other sites More sharing options...
Solution requinix Posted September 21, 2017 Solution Share Posted September 21, 2017 Not only is it possible, it's downright likely. Adjust the delimiter to explode(). 1 Quote Link to comment https://forums.phpfreaks.com/topic/305064-exploding-data-and-putting-it-through-an-if-loop/#findComment-1551700 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. Quote Link to comment https://forums.phpfreaks.com/topic/305064-exploding-data-and-putting-it-through-an-if-loop/#findComment-1551703 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. Quote Link to comment https://forums.phpfreaks.com/topic/305064-exploding-data-and-putting-it-through-an-if-loop/#findComment-1551709 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. Quote Link to comment https://forums.phpfreaks.com/topic/305064-exploding-data-and-putting-it-through-an-if-loop/#findComment-1551710 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. Quote Link to comment https://forums.phpfreaks.com/topic/305064-exploding-data-and-putting-it-through-an-if-loop/#findComment-1551714 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.