0

I have the values in the database IT213|---|IT214|---|IT215 and want to explode those values.

$subject_query=mysql_query("select * from students where id='$get_id'") or die(mysql_error());
$subject_row =mysql_fetch_array($subject_query);
$explode = explode("|---|", $subject_row['subject']);
foreach($explode as $subjects)
    {
    $query=mysql_query("select * from subject where code != ('$subjects') ") or die(mysql_error());
    }
while($row=mysql_fetch_array($query)) { ?>
    <option> 
    //echo to show values that are not included in the database
    </option>
    <?php }  ?>

How do i not include the values to be echoed from the list of values that are already in my database?

For example, i already have IT215 in my database, i want my option box to not include IT215 in the Option box.(Selections are from IT215, IT216, IT217 down to IT216 and IT217 only)

Eric
  • 13
  • 6
  • Can you rephrase the question, or show what you don't want, and what you do want? – chris85 Jul 08 '17 at 17:03
  • i added an example in the last part of the text. sorry for the confusion. – Eric Jul 08 '17 at 17:12
  • Eric, you need to ***STOP*** using `mysql_` functions in PHP and instead use `mysqli_` functions or `PDO` connections to your database. `mysql_` is no longer maintained, is DEPRECATED and a security risk, using it will encourage you to learn bad habits and not learn better habits of coding. Please take some time to learn and use the newer functions!! – Martin Jul 08 '17 at 17:20
  • 1
    I'm sorry to use mysql_ functions since i only learned my knowledge in php on my own in some old php sites and nobody oriented me unto that.(Until you did). Will do it in the future. Thank you. – Eric Jul 08 '17 at 17:25
  • @Eric [try this page](http://codular.com/php-mysqli) it should give you a good basic guide to converting yourself to using `mysqli_`. Good luck wth it `:-)` . Also [this SO topic](https://stackoverflow.com/questions/1390607/how-could-i-change-this-mysql-to-mysqli) may be useful, note that people where changing from MySQL to MySQLi **7 years ago!** – Martin Jul 08 '17 at 17:30

1 Answers1

2

Queries can do plenty of powerful things.

SELECT * FROM subject
WHERE FIND_IN_SET(code, (
    SELECT REPLACE(subject, '|---|', ',')
    FROM students
    WHERE id = ?
)) = 0

Of course ideally I'd tear your database apart and normalise it so that you can achieve the result with joins instead, but I'll take what I can get.

I'd also probably slap you for using functions that don't even exists anymore because they were so bad. Pick up PDO, it's so much better.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • hahaha, your comments underneath the code made me laugh. So true, so very true. Have a plus one for your efforts `:-)` – Martin Jul 08 '17 at 17:22