Jump to content

Conditional Statements on Large Page of Data


tec-4

Recommended Posts

I've got a large page with input data that is being pulled dynamically from a database and am wondering if I could simply my approach, or if what I am doing is correct.  Just seems like it's getting a bit cumbersome as the page continues to get larger and larger.

 

For example, I have index.php page being used to display a bunch of product pages and have, for example, 100 pieces of data broken down into sections (price, amenities, interior, exterior, etc).  There are between 3-15 pieces of data per section and am wondering if there is an easier/less invasive way to make them not populate if the database is empty

 

What I'm currently doing:  I'm currently wrapping each data row in an IF statement to only display it if data is in the database, then I have a larger conditional statement around the entire sections so that if no data displays in any of the fields then the entire section would disappear - if that makes sense...This is probably the easiest approach but am wondering if any of you do it different to handle code, etc.

 

Small Example Code:

 

 

            <?PHP if ($data->row1 || $data->row2) { ?>

            <div class="data_example">

              <h2>Interior</h2>

             

            <?PHP if ($data->row1){?>               

              <dl class="data_example">

                <dt>Interior Misc:</dt>

                <dd><?=preg_replace('/[,]/', ', ', $data->row1);?></dd>

              </dl>

            <?PHP }?>

           

            <?PHP if ($data->row1){?> 

              <dl class="data_example">

                <dt>Floors: </dt>

                <dd><?=$data->row2?></dd>

              </dl>

            <?PHP }?>

          </div>

          <?PHP }?>

 

 

Link to comment
Share on other sites

As long as these sections follow a consistent format, then you could do all this programmatically. I would create an array of the structure of the feilds by session and then run a loop to do all of this. Let me take a few minutes to bang out an example/

Link to comment
Share on other sites

I have a few things to make your code easier to read. Firstly, don't use uppercase in your php tags. Secondly, don't use shorthand tags (<?=blah?>) as not all server configurations have this enabled, so you may run into problems and it's generally bad practice.

 

When working with PHP and HTML in the way you are, I find using if(): endif; to be prettier and more precise, so you know which closing brackets belong to what statement. You'd do it like this:

 

<?php if ($data->row1 || $data->row2): ?>
  <div class="data_example">
    <h2>Interior</h2>
    
  <?php if ($data->row1): ?>                
    <dl class="data_example">
      <dt>Interior Misc:</dt> 
      <dd><?php echo preg_replace('/[,]/', ', ', $data->row1); ?></dd> 
    </dl>
  <?php endif; ?>
  
  <?php if ($data->row1): ?>  
    <dl class="data_example">
      <dt>Floors: </dt>
      <dd><?php echo $data->row2; ?></dd>
    </dl>
  <?php endif; ?>
</div>
<?php endif; ?>

 

This also works with loops, like this:

<?php foreach($array as $val): ?>
     stuff here
<?php endforeach; ?>

 

One more thing. You are simply checking if($data->row1). If this is false then you'll get an error saying that row1 isn't defined, if you have all errors on (error_reporting(-1)), which you should.

 

So do this instead

if (isset($data->row1)

Link to comment
Share on other sites

Using the current layout as you described above here is a way you could build your page automatically. Just set up the two arrays as needed then the rest of the code will do all the work

<?php

//Array for section/subsection structure
$sectionsAry = array(

    'Interior' => array(
        'Interior Misc' => 'row1',
        'Floors' => 'row2'
    ),

    'Header Section2' => array(
        'Subhead 1 Sec 2' => 'row3',
        'Subhead 2 Sec 2' => 'row4'
    ),
);

//Array to hold formatting rules for values
$subsecFormats = array(
    'Interior Misc' => array ('match'=>'/[,]/', 'replace'=>', ')
);


//Run through each section and subsections
foreach($sectionsAry as $sectionName => $subsectionsAry)
{
    //Create variable to hold the HTML output for the subsection
    $subsectionHTML = '';
    //Process the subsections to see if there is data
    foreach($subsectionsAry as $subsecName => $subsecKey)
    {
        //Check if subsectionhas a value
        if($data->$subsecKey)
        {
            //Generate the HTML for the subsection
            $subsecValue = $data->$subsecKey;
            //Check if the value needs to be formatted
            if(in_array($subsecName, $subsecFormats))
            {
                $match   = $subsecFormats[$subsecName]['match'];
                $replace = $subsecFormats[$subsecName]['replace'];
                $subsecValue = preg_replace($match, $replace, $subsecValue);
            }
            $subsectionHTML .= "<dl class=\"data_example\">\n";
            $subsectionHTML .= "<dt>{$subsectionName}:</dt>\n";
            $subsectionHTML .= "<dd>{$subsecValue}</dd>\n";
            $subsectionHTML .= "</dl>\n";
        }
    }
    //Processing of all subsections complete
    //Check if any had subsections had values
    if($subsectionHTML != '')
    {
        //Show section header
        echo "<div class=\"data_example\">\n";
        echo "<h2>{$sectionName}</h2>\n";
        //Ouptu subsectioncontent
        echo $subsectionHTML;
        //Close div
        echo "</div>\n";
    }
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.