Hrvoje Posted January 6, 2016 Share Posted January 6, 2016 (edited) Hello, need some help. I have this code to see if there is any 0 number in field. <?PHP $boja1="divclass1"; $boja2="divclass2"; $provjera="SELECT kolicina FROM stanje_lijekova WHERE kolicina= 0"; if (!$q=mysql_query($provjera)) { echo "Error" . mysql_query(); die(); } if (mysql_num_rows($q)>0) { $boja1; } else { $boja2; } ?> How can I make this code to echo one of two variables with div class. If row have number 0 in field than my class need divclass1 if any other number than is divclass2. <div class='here i need that echo'>some code</div> And my dba: This is classic mouse hover css: Edited January 6, 2016 by Hrvoje Quote Link to comment Share on other sites More sharing options...
requinix Posted January 6, 2016 Share Posted January 6, 2016 if (mysql_num_rows($q)>0) { echo $boja1; } else { echo $boja2; }Or maybe I don't understand the question? Quote Link to comment Share on other sites More sharing options...
Hrvoje Posted January 6, 2016 Author Share Posted January 6, 2016 My mistake with mysql_num_rows. If there is any 0 in any field I want to echo diferent color to div class. Like in last picture. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 6, 2016 Share Posted January 6, 2016 Then your code doesn't match up with your screenshot? Easiest way: do the query to get the data you want, like normal. Maybe SELECT naziv_lijeka, sifra, jedmjera, kolicina FROM stanje_lijekova WHERE some conditionThen do another query that looks similar but gets a count of how many rows have kolicina=0. SELECT COUNT(1) FROM stanje_lijekova WHERE some condition AND kolicina = 0The first value in the first row will tell you how many there are. So don't use mysql_num_rows() for it. Quote Link to comment Share on other sites More sharing options...
Hrvoje Posted January 7, 2016 Author Share Posted January 7, 2016 Ok, I understand this. Thanks I know how to select all rows with value '0'. But how to echo ex... color1 for records that have 0 in field kolicina? Quote Link to comment Share on other sites More sharing options...
Hrvoje Posted January 7, 2016 Author Share Posted January 7, 2016 This is code that show that records: <?PHP include("config.php"); $sql="SELECT * FROM stanje_lijekova ORDER BY naziv_lijeka ASC"; if (!$q=mysql_query($sql)) { echo "Error" . mysql_query(); die(); } if (mysql_num_rows($q)==0) { echo "No data!"; } else { while ($redak=mysql_fetch_array($q)) { echo " <div class='podatak'> <div class='naziv_lijeka_p'><p class='polje_tekst'> ".$redak['naziv_lijeka']."</p></div> <div class='sifra_p'><p class='polje_tekst'> ".$redak['sifra']."</p></div> <div class='jed_mjera_p'><p class='polje_tekst'> ".$redak['mjera']."</p></div> <div class='kolicina_p'><p class='polje_tekst'> ".$redak['kolicina']."</p></div> <div class='ostalo_p'><p class='polje_tekst'></p></div> </div>"; }}; ?> I apologise for wrong code upper. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 7, 2016 Share Posted January 7, 2016 Alright so we're back to my original interpretation of your question. $redak['kolicina'] is the value. If the value is 0 then output one class, otherwise output another class. And as you already know the HTML markup will look like So put the right class name in there. Quote Link to comment Share on other sites More sharing options...
Hrvoje Posted January 7, 2016 Author Share Posted January 7, 2016 How to define that two classes in php and echo it. How to integrate it: ex... $color1="#000000"; $color2="#FFFFFF"; Quote Link to comment Share on other sites More sharing options...
requinix Posted January 7, 2016 Share Posted January 7, 2016 Okay. So. echo " <div class='podatak'>That's where the class needs to go. You already have $boja1="divclass1"; $boja2="divclass2";but I don't know which is the normal class and which is the highlight class so I'm just going to guess. Use an if to decide which class you should use. if ($redak['kolicina'] == 0) { $boja = $boja1; } else { $boja = $boja2; }Now put that variable into the HTML. echo " <div class='podatak {$boja}'> Quote Link to comment Share on other sites More sharing options...
Hrvoje Posted January 7, 2016 Author Share Posted January 7, 2016 Thanks, finaly got it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.