I have a field in my database table: TypeID, which contains values 1,2,3 etc.

How would I set a condition for the following:

i.e. The filed contains 2,3

Display image2 if field contains "2"

Diplay image 3 if field contains "3"

So because the field contains a 2 and a 3, I want to display image2 and image3

    brettacvh wrote:

    I have a field in my database table: TypeID, which contains values 1,2,3 etc.

    It is usually wrong to have such multi-valued fields in a relational database.

    An immediate fix probably involves the use of [man]explode/man, but a better fix would be to change your database design such that you do not have fields that contain such comma separated values. Rather, you would have multiple rows, possibly even introduce another table if necessary.

      Thanks laserlight. Normally I would've built the database properly, however I did not build it, and had to use what was already built. There are also too many records for me to build another table and change all the values. I know I need to use 'explode', but am not quite sure how to implement it into the conditional region.

        A hypothetical example:

        $row['field'] = '2,3';
        foreach (explode(',', $row['field']) as $value) {
        	display('image' . $value);
        }

          Though I understand what you have written, it does not work. It makes my page display as blank.

            This is what I tried:

            $row = $row_shop_RS['TypeID'];
            foreach (explode(',', $row) as $value) {
            
            echo('../images/tick' . $value . '.gif');

              You might want to check that $row_shop_RS['TypeID'] contains the expected data, and that the URL that is output is correct.

                Yep. Both are correct. Hence my confusion.

                  hmm... what exactly do you mean by "it makes my page display as blank"? Do you have display_errors set to on and error_reporting set to at least E_ALL?

                    After putting this code in, the browser displays a blank page, with no errors.

                      Is something like this closer?

                      <?php
                      
                      $is1 =  substr_count($row_shop_RS['TypeID'],"1");  
                      $is2 = substr_count($row_shop_RS['TypeID'],"2");
                      $is3 = substr_count($row_shop_RS['TypeID'],"3");
                      $is4 = substr_count($row_shop_RS['TypeID'],"4"); if $is1 || $is2 || $is3 || $is4 ) { ?>

                      // ... do stuff
                      <img src="../cyber_images/tick.gif" alt="ticked" width="20" height="20" border="0" />

                      <?php } 
                      ?>

                      Though this gives me a blank page too 🙁

                        brettacvh wrote:

                        After putting this code in, the browser displays a blank page, with no errors.

                        Test with this script:

                        <?php
                        $row = '1,2';
                        foreach (explode(',', $row) as $value) {
                        	echo '../images/tick' . $value . '.gif' . "<br />\n";
                        }

                        If you do not get this output:

                        ../images/tick1.gif
                        ../images/tick2.gif

                        then something is wrong with your server configuration.

                        brettacvh wrote:

                        Is something like this closer?

                        No. It is terribly clumsy and inflexible to try and anticipate the possible values like that, and substr_count() can double count, e.g., a string containing '12' could be interpreted as containing '1', '2' and '12'.

                          Thanks for all your help laserlight. I finally worked it out with the following code:

                          <?php 
                          
                          $string_referrer = $row_shop_RS['TypeID'];  
                          
                          $search_engines = array("1", "2", "3", "4");  
                          
                          if (in_array($string_referrer, $search_engines)) {  
                          
                          ?>
                          
                          <img src="../cyber_images/tick.gif" alt="ticked" width="20" height="20" border="0" />  
                          
                          <?php 
                          
                          }  
                          
                          ?> 

                            actually I spoke too soon. This just displays the image for everything.

                              Is this any closer???

                              <?php 
                              array = $row_shop_RS['TypeID'];  
                              if (in_array("1", $type)) {
                              ?> <img src="../cyber_images/tick.gif" alt="ticked" width="20" height="20" border="0" />
                              <?php }
                              ?>

                                And the solution is:

                                <?php
                                $mystring = $row_shop_RS['TypeID'];
                                $findme   = '2';
                                $pos = strpos($mystring, $findme);
                                
                                if ($pos === false) {
                                
                                } else {
                                    echo "<img src='../cyber_images/tick.gif'/>";
                                ;
                                }
                                ?>

                                  ....which will work up until the point where you have a "10" in the field, and you're only looking for "1".

                                    Good point. How would you go about solving that?

                                      Assuming I can't fix the database.

                                      <?php
                                      $mystring = explode(',',$row_shop_RS['TypeID']);
                                      //It's such a bad way of doing things I wouldn't be surprised if it's worse
                                      //than given and there are spaces around the commas.
                                      $mystring = array_map('trim', $mystring);
                                      $findme   = '2';
                                      $pos = in_array($findme, $mystring);
                                      
                                      if ($pos === false) {
                                      
                                      } else {
                                          echo "<img src='../cyber_images/tick.gif'/>";
                                      ;
                                      }
                                      ?> 
                                        Write a Reply...