-2

I am storing a string in a mysql database in the following format:

 $row["actions"]=   Add&&Remove&&Replace&&Change&&

I need to splice each one of those into a check box html form for example

  while{
    echo  $row["actions"];
     //this would be the string to cut up

     }
for each before && as cut {
     <form action="" method="post">
 //name would be add
      <input type="checkbox" name='$cut[0]' value="Bike"> I have a bike<br>
//name would be Remove
 <input type="checkbox" name='$cut[1]' value="Bike"> I have a bike<br>
//name would be replace
   <input type="checkbox" name='$cut[2]' value="Bike"> I have a bike<br>
      <input type="submit" value="Submit">
    </form>}
  • Add&&Remove&&Replace&&Change&& doesn't seem to have anything to do with your desired output. Please clarify. – Difster Nov 18 '18 at 03:27
  • $row[actions] is add remove and that needs to be cut up and for each that would be cut[0] made adjustments @Difster – dixie motors Nov 18 '18 at 03:28
  • `$cut = explode('&&', $row['actions']);` – Nick Nov 18 '18 at 03:42
  • 1
    It is bad practice to store multiple values in a single column in your database. Please make sure you read https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad – Mike Nov 18 '18 at 04:06
  • Your question is not clear, please let us know what is the expected output look like. Thanks – Haktan Suren Nov 18 '18 at 04:25

1 Answers1

0

Why cannot you just use simple check, since you have pre-defined actions?

// If you have "Add" in your string, then print needed string
if (strpos( $row["actions"], 'Add') !== false) {
    echo "<input type=\"checkbox\" name='Add' value=\"Bike\"> I have a bike<br>"
}
//... and so on`

And if you really need to split and iterate over your values, then take a look at explode function, which does exactly this. Documentation.

It is not really clear what you're trying to achieve. As already noted in comment, you should neither:

  • Store multiple values in one row in the database \ in one variable
  • Have checkboxes for mutually exclusive actions (?). Probably you want radiobuttons here.

Also please note that your code is not a valid PHP code. You cannot mix HTML and PHP like this. So you need either to close PHP tag like <?php $a=1; ?> <a href="google.com">HTML goes here</a> or do echo or print: echo "<a href=\"google.com\">Some HTML</a>";

The Godfather
  • 4,235
  • 4
  • 39
  • 61