How to show or hide a code based on value in database?

Discussion in 'PHP' started by popokolok, Feb 24, 2013.

  1. #1
    Hello,

    I have this link code:
    <a href="<?php if($row->pagetitle=="")
                            {
                                echo $this->config->item('url').'widget/edit/'.$row->widgetid;
                            }
                            else
                            {
                                echo $this->config->item('url').'widget/edit2/'.$row->widgetid;
                            }
    ?>"><img src="<?php print $this->config->item('url').'images/edit.png'; ?>"/></a>
    Code (markup):
    "widgetid" is defined earlier, I basically need a php code that can check the database and to check in a certain table in the row of the defined widgetid for a value in another column, let's call it ZXC, whether the value in that row in that column is 1 or null, if 1 show the above script, if null hide it.

    I hope I've made it rather clear, tell me if you need more info and please help me out.

    Thanks!!
     
    popokolok, Feb 24, 2013 IP
  2. Jay-S

    Jay-S Member

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #2
    This should work:
    <?php
    if($row->ZXC==1) {
    ?>
     
    <a href="<?php if($row->pagetitle=="")
                            {
                                echo $this->config->item('url').'widget/edit/'.$row->widgetid;
                            }
                            else
                            {
                                echo $this->config->item('url').'widget/edit2/'.$row->widgetid;
                            }
    ?>"><img src="<?php print $this->config->item('url').'images/edit.png'; ?>"/></a>
     
    <?php
    }
    ?>
    PHP:
     
    Jay-S, Feb 24, 2013 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    1) it's stupid to be opening and closing PHP so much for no good reason.

    2) 90% of the output results inside the IF are the same, so they don't belong inside a conditional. If the only thing different between the two conditons is 'edit' vs. 'edit2' -- those should be the only things inside that condition.

    3) It's usually better to check for empty instead of ==''

    4) if you're gonna have null as one of the conditions, test for isset so it's not tossing a warning in your logs.

    <?php
    
    if (isset($row->ZXC)) {
    	echo '
    		<a href="',$this->config->item('url'),'widget/',(
    			empty($row->pagetitle) ? 'edit/' : 'edit2/'
    		),$row->widgetid,'">
    			<img src="',$this->config->item('url'),'images/edit.png" alt="edit" />
    		</a>';
    }
    
    ?>
    Code (markup):
    Outer if for the new var seeing if it's not-null, single echo statement handling the ENTIRE output with no dipping in and out of PHP like a script-tard, delimited echo instead of string additions, evaluation instead of an if statement inside the output with the only thing the output changing being the parts that are ACTUALLY different.
     
    deathshadow, Feb 25, 2013 IP