-1

I'm trying to create a menu that will only populate links based on what the user has subscribed to. Basically I will be using php to check the user against a table that will contain his/her email + registered courses in a boolean format ie:

email          | course 1 | course 2 | course 3|
john@email.com   TRUE       FALSE       TRUE

I want to create an if statement that if course 1 = true WHERE email = john@email.com then echo some html

else check for course 2, etc.

What I'm having trouble with is how would i structure my if statement? Would I use

$sql="Select * FROM courses WHERE user = $user and course1 = TRUE"
$result = mysql_query($sql) or mysql_die($sql)
if($result)
{

echo html

}

or is there another way to do this? Is there a way I can write an if statement to check multiple values for example if($_SESSION['auth_id'] == 1) and if (course1 == TRUE) (i know that the last if statement isnt correct but I'd like to combine the two if possible). Would I need to write it as an if statement within an if statement:

if ($something)
{
   if ($somethingelse)
    {
       do work
     }
}

or an elseif:

if ($something)
{
   elseif ($somethingelse)
    {
       do work
     }
}
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
maestro416
  • 904
  • 2
  • 17
  • 31
  • You don't have any plan nor is this your code, right? Can you explain where $_SESSION['auth_id'] comes from? Also your database structure is not normalized ... – djot Sep 08 '13 at 03:04
  • Don't use the deprecated mysql* functions and don't build queries by concatenation. – Wooble Sep 08 '13 at 03:05
  • @ Fred -ii- i didn't know I was being graded on semicolons. I just put it together really quickly to make a point. If you want to help - be constructive not annoying – maestro416 Sep 08 '13 at 03:07
  • @Fahad Ten you should have put up actual code and not just "You (as in me/us) Fill in the Blanks) and take care of the rest. You ought to know better. – Funk Forty Niner Sep 08 '13 at 03:09
  • @djot auth_id is from after the user logs in. They're basically divided in to certain groups or categories. So the page will be checking initially to make sure user is logged in, but there will be multiple pages that will have to check the user category and if they can access specific courses. – maestro416 Sep 08 '13 at 03:10
  • @Fred-ii- I'm asking how to structure an if statement, not for you to write everything for me. It's a simple question. If you don't know the answer, don't take up comment space with silliness – maestro416 Sep 08 '13 at 03:11
  • @Fahad *"I'm trying to create a menu"* - You shown us no actual code that you're having difficulty with. So IMO, your question didn't show any effort to resolve/build what you need to achieve. Just a lot of `IFs` and `do work`. – Funk Forty Niner Sep 08 '13 at 03:15
  • 2
    Really... you want to see the html as well or just the relevant part that isn't working. If you want more information be like everyone else and ask for it.. dont be condescending. Instead of recommending Codes-R-Us to others, you should take some courses on people skills. – maestro416 Sep 08 '13 at 03:19
  • @Fahad I doubt very much that I was "condescending" as you call it. Re-read my comments. If you can't take creative criticism, then you're in the wrong business. [Here](http://php.net/manual/en/control-structures.if.php) and [here](http://php.net/manual/en/control-structures.elseif.php) are where you'll find most of your answers on how to construct `if/else`. Plus Google isn't broken nor offline. – Funk Forty Niner Sep 08 '13 at 03:21

5 Answers5

1

Get the row with the desired ID with all three courses. Then use PHP to check each of the course values and output the desired HTML accordingly.

To combine if statements, do this (you were close):

if (condition1 AND condition2)... 
elseif (condition3)... 

Or use && instead of AND, they'll work the same in your case, but they are not the same because of precedence. I'm writing you pseudo language that's a bit easier to read.

You can have as many conditions as you want:

if ( ((c1 OR c2) AND (!c3 AND c4) AND !(c5 AND c6)) OR c7 )... 

And don't forget to stop using the mysql_* functions as they are deprecated. Switch to mysqli or PDO instead.

Community
  • 1
  • 1
Shomz
  • 37,421
  • 4
  • 57
  • 85
1

You're asking how to do multiple conditions in an if in PHP, I see.

The boolean operators && (AND) and || (OR) will be your friends here.

if($cow=='brown'){
    if($cat=='white'){
        doStuff();
        }
    }

is the same as

if($cow=='brown' && $cat=='white'){
    doStuff();
    }

The && in the middle means the condition is only true overall if both parts are true.

see Logical Operators

The second one, with elseif, would be a use for ||. The wexample you gave would not work (it would never reach the elseif if the first one were not true...) but these two are equivalent:

 if($something){
    doStuff();
    }
 elseif($somethingElse){
    doStuff();
    }

and

 if($something || $somethingElse){
    doStuff();
    }

it will doStuff() if either one is true.

JAL
  • 21,295
  • 1
  • 48
  • 66
1

About logical operators:

Example ----- Name---Result

$a and $b---And-----TRUE if both $a and $b are TRUE.

$a or $b ----Or-------TRUE if either $a or $b is TRUE.

$a xor $b-- Xor------TRUE if either $a or $b is TRUE, but not both.

! $a-----------Not------TRUE if $a is not TRUE.

$a && $b----And------TRUE if both $a and $b are TRUE.

$a || $b----Or--------TRUE if either $a or $b is TRUE.


The returned $result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data

Something like this:

while ($row = mysql_fetch_assoc($result)) {
  $userid=$row["userid"];
  $course1 =$row["course1"];
  if($userid==1 && course1){
    //do something
  } 
}

Get result

As side note: mysql_* functions are deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

mysqli_fetch_assoc()

PDOStatement::fetch(PDO::FETCH_ASSOC)

enter image description here

Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
1

This is NOT intended as an answer, it's just that it's too big for a comment.

It looks like you are pretty new to all of this. We all were, once, so I wish you luck. Stick with it and it will become clear and easier (and you will build up a good collection of code snippets).

Read a few books. That really got me up to speed with PHP and MySql.

And be sure that you know when to use MySql and when to use Sqlite. I mention this because, although PHP has some slight preference for MySQl, it also happilly supports Sqlite. And, if you think you might ever develop for Android or Ios then it might be better to become an Sqlite guru as both of them support Sqlite, but neither support MySql.

Get a good IDE. I personally love Netbeans. It's coding & debug facilities are excellent. For development on your own PC you need an Apache server and MySql; I don't think you can beat Xampp if you are using Windows (it's even easier with Linux).

But, I digress, I only started this to mention that you might want to consider your database design. It works fine so long as you are 100% certain that you will never have more than 3 courses.

Even then, it is not third normal form.

I would probably start with something like this. You can add more columns as needed and I will leave it to you to sort out the Primary and Foreign Keys.

mysql> show tables;
+------------------------+
| Tables_in_menu_example |
+------------------------+
| course_subscription    |
| courses_description    |
| students               |
+------------------------+
3 rows in set (0.00 sec)

mysql> describe students;
+------------+---------+------+-----+---------+----------------+
| Field      | Type    | Null | Key | Default | Extra          |
+------------+---------+------+-----+---------+----------------+
| student_id | int(11) | NO   | PRI | NULL    | auto_increment |
| email      | text    | NO   |     | NULL    |                |
+------------+---------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> describe courses_description;
+-------------+---------+------+-----+---------+----------------+
| Field       | Type    | Null | Key | Default | Extra          |
+-------------+---------+------+-----+---------+----------------+
| course_id   | int(11) | NO   | PRI | NULL    | auto_increment |
| course_name | text    | NO   |     | NULL    |                |
+-------------+---------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> describe course_subscription;
+------------+---------+------+-----+---------+-------+
| Field      | Type    | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| student_id | int(11) | NO   |     | NULL    |       |
| course_id  | int(11) | NO   |     | NULL    |       |
+------------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

I do realize that this hasn't answered your question at all (sorry), but I hope that it will help some. Good luck!!

Community
  • 1
  • 1
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • Actually this was a big help, as I have come to a point with the DB where I'm not 100% certain of the structure anymore and it's looking messy - currently my plan was that I would have 2 main tables and then a separate table per course category. The 2 main tables are users which would store user info, and then 2nd would be courses (all the courses that are offered, along with one column for email/username to show which user has subscribed to which course). Then there would be a third (or many more) table that would have all the chapters in a course along with their descriptions, urls, etc. – maestro416 Sep 08 '13 at 07:46
  • I would basically use the php from the question above to push out the chapters based on the following sequence of authentication: user-->course subscription ---> Course Chapters, and those chapters would basically populate in a list for users to click on and view. The problem is, I have A LOT of courses for subscription, and it'll grow in the future. This is meant to stream tutorial videos on things like math, chem, bio, etc. So I'm not entirely sure where to take the database structure. At this point im doing some research on it – maestro416 Sep 08 '13 at 07:49
0

try to use *mysql_num_rows.*

example:

    $syntax = mysql_num_rows(//your query syntax);
if($syntax >0 )
{
  // do return
}
else
{
  // refresh
}
C.Gembel
  • 1
  • 1