If statement to display articles based on the category

7 posts by 2 authors in: Forums > CMS Builder
Last Post: October 13, 2014   (RSS)

By Jesus - October 8, 2014

Hi,

I'm working with my blog and I just came to a point where I'm lost.... ok, here's my situation. I've a blog with 4 categories, and I'm trying to display the latest articles per category on the sidebar inside my pagedetails page. So I'm assuming I need to use an IF statement to do this.

On my header I've this:

  // load record from 'blogarticulos'
  list($blogarticulosRecords, $blogarticulosMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'where'    => "categoria='Articulos'",
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '15',
  ));

  // load record from 'blogdimequecomes'
  list($blogdimequecomesRecords, $blogdimequecomesMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'where'    => "categoria='Dime que comes'",
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '15',
  ));

and inside my page where I need to display my articles I've this:

<?php if (!$blogarticulosRecords): ?>
              <h4>Art&iacute;culos</h4>
              <ul class="nav-list-primary">
    <?php foreach ($blogarticulosRecords as $record): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></li>
    <?php endforeach ?>
              
    <?php if (!$blogarticulosRecords): ?>
      <li>Por el momento no hay art&iacute;culos disponibles.</li>
    <?php endif ?>

<?php elseif (!$blogdimequecomesRecords): ?>
              <h4>Dime que comes</h4>
              <ul class="nav-list-primary">
    <?php foreach ($blogdimequecomesRecords as $record): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></li>
    <?php endforeach ?>
              
    <?php if (!$blogdimequecomesRecords): ?>
      <li>Por el momento no hay art&iacute;culos disponibles.</li>
    <?php endif ?>
<?php endif ?>  

The key here its the category value.

IF articulos will display the latest articles from the articulos category

IF dimequecomes will display the latest articles from the dimequecomes category

Thanks in advance for pointing me to the right direction on this,

Jesus

By Damon - October 9, 2014

Hi,

Try this code with the explanation mark removed from both the if and else if statements:

<?php if ($blogarticulosRecords): ?>
              <h4>Art&iacute;culos</h4>
              <ul class="nav-list-primary">
    <?php foreach ($blogarticulosRecords as $record): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></li>
    <?php endforeach ?>
              
    <?php if (!$blogarticulosRecords): ?>
      <li>Por el momento no hay art&iacute;culos disponibles.</li>
    <?php endif ?>

<?php elseif ($blogdimequecomesRecords): ?>
              <h4>Dime que comes</h4>
              <ul class="nav-list-primary">
    <?php foreach ($blogdimequecomesRecords as $record): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></li>
    <?php endforeach ?>
              
    <?php if (!$blogdimequecomesRecords): ?>
      <li>Por el momento no hay art&iacute;culos disponibles.</li>
    <?php endif ?>
<?php endif ?> 

Let me know if this works for you.

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Jesus - October 9, 2014

Hi Damon, thanks for your answer.

ok, I'm displaying the articles headlines for the 1st category on both cases.

 Which means, probably that this code its not working properly

  // load record from 'blogarticulos'
  list($blogarticulosRecords, $blogarticulosMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'where'    => "categoria='Articulos'",
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '15',
  ));

  // load record from 'blogdimequecomes'
  list($blogdimequecomesRecords, $blogdimequecomesMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'where'    => "categoria='Dime que comes'",
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '15',
  ));

or

The where statement its not working

or

I'm missing something on the IF statement.

We are making progress because before removing the exclamation point, no items were displayed.

Thanks for your help!

By Jesus - October 13, 2014

I've an error on this IF statement, if someone can give me a hand on this will be awesome :)

Thanks

<?php if (categoria='Articulos'): ?>
              <h4>Art&iacute;culos</h4>
              <ul class="nav-list-primary">
    <?php foreach ($blogarticulosRecords as $record): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></li>
    <?php endforeach ?>
              
    <?php if (!$blogarticulosRecords): ?>
      <li>Por el momento no hay art&iacute;culos disponibles.</li>
    <?php endif ?>

<?php elseif (categoria='Dime que comes'): ?>
              <h4>Dime que comes</h4>
              <ul class="nav-list-primary">
    <?php foreach ($blogdimequecomesRecords as $record): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></li>
    <?php endforeach ?>
              
    <?php if (!$blogdimequecomesRecords): ?>
      <li>Por el momento no hay art&iacute;culos disponibles.</li>
    <?php endif ?>
<?php endif ?> 

By Damon - October 13, 2014

Hi,

The changes I made to your code are in bold. Try this and it should work for you. Note that there are two equal symbols (==).

<?php if ($record['categoria'] == "Articulos"): ?>
              <h4>Art&iacute;culos</h4>
              <ul class="nav-list-primary">
    <?php foreach ($blogarticulosRecords as $record): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></li>
    <?php endforeach ?>
              
    <?php if (!$blogarticulosRecords): ?>
      <li>Por el momento no hay art&iacute;culos disponibles.</li>
    <?php endif ?>

<?php elseif ($record['categoria'] == "Dime que comes"): ?>
              <h4>Dime que comes</h4>
              <ul class="nav-list-primary">
    <?php foreach ($blogdimequecomesRecords as $record): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></li>
    <?php endforeach ?>
              
    <?php if (!$blogdimequecomesRecords): ?>
      <li>Por el momento no hay art&iacute;culos disponibles.</li>
    <?php endif ?>
<?php endif ?>

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Jesus - October 13, 2014

Awesome!

Thanks!