-2

One of the first database outputs and i'm pretty much stuck. I want to add different classes to an array of information. The array has categories and that will be the leading value to add the class to. The code i'm using so far is:

<?php

    $sql = "SELECT name,title,content,date,category FROM pinboard";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {

        // output data of each row
        while($row = $result->fetch_assoc()) {

            echo "<article class='pin'><h2 class='pintitle'>".$row["title"]."</h2><p class='pincontent'>".$row["content"]."</p><div class='pininfo'><p class='pinname'>".$row["name"]."</p><p class='pindate'>".$row["date"]."</p></div></article>";

            if($row['category'] = 2){

                $('.pin').addClass("pin-message");
            }
            else if($row['category'] = 1){
                $('.pin').addClass("pin-photo");
            }
            else if($row['category'] = 3){
                $('.pin').addClass("pin-event");
            }
        }

    } else {
        echo "0 results";
    }
?>

The if($row[category'] = 2){ line gives this result when it's displayed:

Parse error: syntax error, unexpected '(', expecting variable (T_VARIABLE) or '$' in C:\wamp\www....\index.php on line ..

I'm probably not identifying the problem correctly but I'm hoping you guys could help me further.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 2
    You seem to be mixing javascript with php. And your comparissons are assignments... – jeroen May 15 '16 at 18:52
  • I have down voted this question: => The problem has nothing todo with MySql directly => It's a absolut base php question => With little effort (debuging...) you will found the problem – Cyril Iselin May 15 '16 at 18:59

1 Answers1

0

You are mixing php with javascript.
And you are making it far more complicated than it needs to be.

Assuming everything above is fine (db-connection, query, ...), this shall be one better way to achieve what you want:

<?php

$sql = "SELECT name,title,content,date,category FROM pinboard";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

  while($row = $result->fetch_assoc()) {
    // define css-classes in php already
    $classes="pin";
    switch ($row['category']) {
        case 1:
            $classes .= " pin-photo";
            break;
        case 2:
            $classes .= " pin-message";
            break;
       //...and so on
    }
    // and then simply display them in first place
    echo "<article class='".$classes."'><h2 class='pintitle'>".$row["title"]."</h2><p class='pincontent'>".$row["content"]."</p><div class='pininfo'><p class='pinname'>".$row["name"]."</p><p class='pindate'>".$row["date"]."</p></div></article>";
    // no need for any js-dom-manipulation afterwards
  }
} else {
     echo "0 results";
}
?>

Appendum
you had two major misstakes:
1. trying to call js-functions from php
2. wrong comparison: if($x=1) does NOT compare, but assign 1 to $x. Thus if will always be true. Correct is $x===1 or $x==1 (that difference is another topic...)

Jeff
  • 6,895
  • 1
  • 15
  • 33
  • Thanks alot for replying that quickly with a good explanation. I tried out your method but I still keep getting parse errors. Now its the brackets } at the end that creates the error. I'm so sorry for my novice in programming but i just can't seem to figure out what to do with the parse errors. – Bas Beton May 16 '16 at 07:52
  • I've changed my answer and included the whole script now. This should not give any parse errors. If you get errors, spot the line, make sure the brackets match. Sometimes the error line is one line _after_ the actual misstake! – Jeff May 16 '16 at 10:10