Forum Moderators: coopster

Message Too Old, No Replies

Hide HTML Table or DIV based on MySQL value

hide table or div based on mysql value

         

zorro

1:13 pm on Jul 20, 2010 (gmt 0)

10+ Year Member



I want to hide a <table> or <div> based on a value in a MySQL database row but can't for the life of me figure it out after 3 days of trying hundreds of different combinations:

A value of '1' is assigned to a database listing as standard and inserted into the table column named 'type'.
If the user of the listing wants to make it 'featured' a value of '2' is assigned to the 'type'.

The table looks like this (simplified):

<table><tr><td>
<script type="text/javascript">
featuredcontentglider script from dynamic drive
</script>
<div id="featured" class="glidecontentwrapper"></div>
</td></tr></table>

I would like to hide the whole table (possibly show another table instead) if the value in the database 'type' column is NOT '2'.

I've tried all sorts of different combinations to only show the table if the value in 'type' column was = to 2 but can't get it to work.

Tried enclosing table in <div> tags
Tried php if statements/ if else / echo etc
Just can't seem to get it to work!
Any help or a point in the right direction would be very much appreciated.

Matthew1980

2:12 pm on Jul 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there zorro,

Thats simple enough, all you need to do is something like this:-

if(VALUE_FROM_DB == "1"){
//show div
}

conversely if you want to invert the action:-

if(VALUE_FROM_DB != "1"){
//Div doesn't show
}

Alternatively post a portion of the code that's bothering you, and we can sort you out from there :)

When you use '=' that's an assignment operator, literally means assign the value to, but using '==' means equal to just like != means not equal to..

Cheers,
MRb

zorro

3:59 pm on Jul 20, 2010 (gmt 0)

10+ Year Member



Hi Matthew,

Thanks very much for your quick response!

I have already tried what you mention (using ==) but it doesn't work. I was just using the single '=' to explain the problem.

Here is the page code (simplified):
-----------------------------------
<?php
session_start();
include("website_options.php");
?>

<DOCTYPE>
<HEAD>
</HEAD>
<BODY>

SOME OTHER STUFF HERE

<tr><td>

<?php
$sql = "SELECT ".$TABLES["accommodations"].".*
FROM ".$TABLES["accommodations"]."
"WHERE ".$TABLES["accommodations"].".date_feature_expiration >= NOW() AND ".$TABLES["accommodations"].".area_id=10 AND ".$TABLES["accommodations"].".type=2 AND ".$TABLES["accommodations"].".accommodation_status like 'Approved'
ORDER BY ".$TABLES["accommodations"].".type DESC LIMIT 0,1";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql_ftr." mysql error:".mysql_error());
?>

<?php if ($ACCOMMODATIONS["type"]=='2') { ?>

<table>THE TABLE IN ORIGINAL POSTING GOES HERE</table>

<?php }; ?>

</td></tr>


HAVE ALSO TRIED THIS...

<?php if ($ACCOMMODATIONS["type"]=='2') { ?>

<table>THE TABLE</table>

<?php } elseif ($ACCOMMODATIONS["type"]=='1') { ?>
<table><tr><td>SOMETHING</td></tr></table>
<?php } else { ?>
<table><tr><td>SOMETHING</td></tr></table>

<?php }; ?>
<?php }; ?>

ALSO TRIED ECHO'S AND LOTS MORE...

rocknbil

4:46 pm on Jul 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would like to hide the whole table (possibly show another table instead) if the value in the database 'type' column is NOT '2'


But you're making sure all of them are 2?

$sql = "SELECT ".$TABLES["accommodations"].".*..."WHERE.... AND ".$TABLES["accommodations"].".type=2

So if you want to make a programmatic decision based on whether it's 1 or 2, you need to eliminate that from your where clause.

You know this selects one record, right?

type DESC LIMIT 0,1";

If you're doing that, you could just be selecting the record by ID?

I assume you're doing this:

$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql_ftr." mysql error:".mysql_error());
while ($ACCOMMODATIONS = mysql_fetch_array($sql_result)) {
// table logic here
}

Otherwise nothing gets into $ACCOMMODATIONS.

zorro

5:48 pm on Jul 20, 2010 (gmt 0)

10+ Year Member



Hi rocknbil,
Thanks for your reply!
I have tried it with type=2 and without in the where clause and neither worked!
Yes I understand that LIMIT 0,1 will only show one result but it is not the amount of results I am concerned with as these are all handled within the javascript (featured content glider from dynamic drive.com) inside the <table>
The reason for using LIMIT 1 is to only show one <table>
If I don't use it, it shows multiple <tables> depending on how many results there are for type 2 in the database, if there are 4 x type 2 results I get 4 <tables>

I also have the while statement too. Sorry I forgot to paste that bit into message above!

I am only using the connection to the mysql database to establish whether there is or isn't any type 2 results.
If there is a type 2 result I want to show the <table> if there isn't I don't want to show the table at all or preferably show another table. What is shown inside the <table> is working fine.

I'm not sure what you mean by placing the table logic between the curly braces?

Here is the code as it stands...

<tr><td>

<?php
$sql = "SELECT ".$TABLES["accommodations"].".*
FROM ".$TABLES["accommodations"]."
"WHERE ".$TABLES["accommodations"].".date_feature_expiration >= NOW() AND ".$TABLES["accommodations"].".area_id=10 AND ".$TABLES["accommodations"].".accommodation_status like 'Approved'
ORDER BY ".$TABLES["accommodations"].".type DESC LIMIT 0,1";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql_ftr." mysql error:".mysql_error());
while ($ACCOMMODATIONS = mysql_fetch_array($sql_result))
?>

<?php if ($ACCOMMODATIONS["type"]=='2') { ?>

<table>THE TABLE</table>

<?php }; ?>

<?php } elseif ($ACCOMMODATIONS["type"]=='1') { ?>
<table><tr><td>SOMETHING</td></tr></table>

<?php } else { ?>
<table><tr><td>SOMETHING</td></tr></table>

<?php }; ?>
<?php }; ?>

</td></tr>

Readie

5:55 pm on Jul 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

<?php

$sql = 'SELECT type FROM table WHERE type = "2" LIMIT 1';
$result = mysql_query($sql);
if(mysql_num_rows($result)) {
?>

Table stuff here

<?php
}

?>

Unless we actually want to use the data, there's no need to do SELECT * - it's an uneccesary memory usage.

We use mysql_num_rows() because in the past I've had issues with using mysql_fetch_row() (which is supposedly the fastest) and then trying to run a while() loop with mysql_fetch_assoc()

zorro

6:34 pm on Jul 20, 2010 (gmt 0)

10+ Year Member



Hi Readie,

Thanks for that!

Just tried it but all it does is remove the table completely, even though there is a couple of type 2's in the database.

CODE:

<?php

$sql = 'SELECT type FROM ".$TABLES["accommodations"]." WHERE type = "2" LIMIT 1';
$result = mysql_query($sql);
if(mysql_num_rows($result)) {
?>

Table stuff here

<?php
}

?>

Readie

6:41 pm on Jul 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've used the logic in that code - quite literally - hundreds of times and it's always worked fine, so I can only surmise that there's an issue with our WHERE clause.

Are there any spaces, line breaks or *anything* in the "type" column with the '2'?

Also, I have to ask because I didn't see it in a quick glance over your code:

Is
$TABLES["accommodations"]
actually set?

Matthew1980

6:57 pm on Jul 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there,

so I can only surmise that there's an issue with our WHERE clause

Right. This begs the question - have you echod the sql query to the screen before it gets sent to the DB to see if it is constructed the way as you are expecting it to be, AND all of its possible permutations?

When you are sure that it is, then you can send the info to the DB, from there you can rule out the possibility of the statement & concentrate on the results you are getting returned DB side...

Cheers,
MRb

zorro

7:33 pm on Jul 20, 2010 (gmt 0)

10+ Year Member



Readie:
Are there any spaces, line breaks or *anything* in the "type" column with the '2'?

NO JUST EITHER A 1 OR A 2

Also, I have to ask because I didn't see it in a quick glance over your code:

Is $TABLES["accommodations"] actually set?
NO SURE WHAT YOU MEAN BY SET BUT THE DATABASE HAS BEEN UP AND RUNNING FOR MONTHS OK JUST WANT TO ADD THIS EXTRA BIT.

Matthew:
"have you echod the sql query to the screen"

HOW DO I DO THIS!

I'M NOT A CODER I AM JUST USING PARTS FROM OTHER PAGES IN THE SITE THAT DO SIMILAR THINGS AND LOTS OF SEARCHING ON GOOGLE.

Readie

7:36 pm on Jul 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add the following line somewhere into your script:

die('<br><br><br>' . $TABLES["accommodations"]);

Is anything printed to the page underneath 3 line breaks?

zorro

7:45 pm on Jul 20, 2010 (gmt 0)

10+ Year Member



yes

(the table prefix)_accommodations

Sub_Seven

10:43 pm on Jul 29, 2010 (gmt 0)

10+ Year Member



Guys I'm sorry to sneak in but I'm having the same problem, I thought this would work

<?php
if(value from `table` WHERE `input field` == "value") { echo "<div>HTML STUFF</div>"; }
?>

But obviously it didn't, could I have any suggestions please?