1

I'm working on a PHP script to make a catalog. The relevant columns in my database are Vid, banner, category and Scategory. Vid is the primary key, banner is the path to my img files, and category and Scategory are both numbers for the their respective ID. Everything is over simplified as I'm just testing the scripts. There is currently 20 records in the table.

<html>
<head>
<style type="text/css">

</style>  
</head>
<body>
<?php

require "config.php";

$sql = "SELECT Vid,banner,category,Scategory FROM display"; 
$result = $conn->query($sql);
$row = $result;
$min = 10;
$max = 20;

while ($row = mysqli_fetch_assoc($result)) {

    implode ('', $row);
    $vid = $row['Vid'];
    $banner = $row['banner'];
    $cid = $row['category'];
    $sid = $row['Scategory'];


    if ($cid = 2 && $sid = 1){  

        echo 
         '
          <div style="display:inline-block;"> 
           <div style="border-color:blue; border-style:solid;">
             <a href="#test'.$vid.'">
             <img src="'.$banner.'" />
           </div>
          </div>';  
        echo $vid;
        echo $cid;
        echo $sid;



        if ($vid % 2 == 0){
            echo '<br>'; 
        }
    }
}


require'close.php';
?>
</body>
</html>

Now the code runs just fine, but it gets strange I use $cid and $sid as conditions in that IF loop. Given there is 20 records, both $cid and $sid have half their values as '1' and half as '2, so when I set the IF conditions I figured it would return 5 records, but it instead returned all 20. When I echo $vid $cid and $sid, it returns the proper $vid, but $sid returns as whatever condition I set it to. For example conditions set to $cid=1 and $sid=2 returns 1:1:2, 2:1:2, 3:1:2 etc.

Now here is where it gets really strange, regardless of the condition set for $cid it returns as '1', if I set it '7' it still returns as '1'. Whereas $sid returns as whatever number is set. Also when I set either condition to null it returns nothing.

So my question is why it's acting the way it is or how it should be written if I'm writing it wrong.

Also as a side question, when I put <a> before <img> it returns the proper ID that's linked to the <img>. But when I put <a> directly after <img>, it returns the ID of the next iteration's row, and the first iteration returns blank. Anyone happen to know why that happens since regardless of their position in the statement it's still part of the same loop iteration?

5
  • 3
    = is assignment. == and === are comparison operators. Commented Sep 2, 2016 at 8:54
  • Some sensible code indentation would be a good idea. It help us read the code and more importantly it will help you debug your code Take a quick look at a coding standard for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. Commented Sep 2, 2016 at 8:55
  • Wow, thanks Jon Stirling, i didn't realize there was a difference. This fixed the problem. Commented Sep 2, 2016 at 9:02
  • 1
    And still the rep vultures flock in behind a simple comment answer Commented Sep 2, 2016 at 9:06
  • Voting to close as off-topic due to a typo Commented Sep 4, 2016 at 18:38

5 Answers 5

1

You are using a single equal and assign the value to the variable. To compare values you have to use == or ===!

What is the difference between == and ===?

See the following condition to see the difference:

2 == "2" -> Equal
2 == 2 -> Equal
2 === "2" -> Not Equal
2 === 2 -> Equal

How you can avoid this on future? - Yoda condition

You can change your conditions to the following to make sure you are using the conditions on the right way. You only shoud use this for equal comparison not for >, <, <=, >=.

//throws an error
if (0 = $isNull) { ... }

//the right one
if (0 == $isNull) { ... }
Sign up to request clarification or add additional context in comments.

Comments

0
  1. == not = in your if statement.
  2. implode line does nothing

1 Comment

Also $row = $result; does nothing useful
0

Use double-equals instead of singles. Not:

if( $foo = 2 )

...but:

if( $foo == 2 )

Your code is changing the values instead of testing them.

Comments

0

Change if ($cid = 2 && $sid = 1) with if ($cid == 2 && $sid == 1). I think you did it only by mistake, since your other if is fine!

Comments

0

In your if statement you're using a single equals operator it needs to be == or ===

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.