How to do 1 only query and distribute the result spread by the code?

0

Well, I have a site where I am in each area using a select, so I think I'm doing more queries than I should, is there any way to get the result done? follows an image of what I'm proposing

<?phptry{$sql="SELECT * FROM anuncios ORDER BY RAND() LIMIT 20";
       $stmt = $DB->prepare($sql);
       $stmt->bindValue(":Nid", intval($_GET["Nid"]));
       $stmt->execute();
       $results = $stmt->fetchAll();
    } catch (Exception $ex) {
      echo $ex->getMessage();
    }
    foreach ($results as $res) {
    $tipo = $res["tipo"];
    switch ($tipo) {
    case 'Imagem':
    echo "<div style='width:720px;height:90px'>
  <a href='".$res["codigo"]."' target='_blank'><img src='img/anuncios/".$res["Nid"]."/".$res["arquivo"]."' height='90' width='720'> </a>
  </div>";
    break;
    case 'Flash':
    echo "<div style='width:720px;height:90px'>
  <embed src='img/anuncios/".$res["Nid"]."/".$res["arquivo"]."' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' height='90' width='720'> 
  </div>";
    break;
    case 'Codigo':
  echo "<div style='width:100%;height:100px'>
  ".$res["codigo"]."
  </div>";
    break;
    }
    } ?>
    
asked by anonymous 02.03.2016 / 19:15

1 answer

2

Assign the results to a variable. Usually in an array. With the data in a variable, just fit them "wherever you want".

See an example:

<?php
$dummy_data = array('anuncio a', 'anuncio b', 'anuncio c', 'anuncio d');
?>

<table border="1">
<tr>
    <td>bla bla</td>
    <td colspan="2"><?php echo $dummy_data[0];?></td>
</tr>
<tr>
    <td>lorem ipsum</td>
    <td><?php echo $dummy_data[1];?></td>
    <td>dumb lol</td>
</tr>
<tr>
    <td>foo bar</td>
    <td><?php echo $dummy_data[2];?></td>
    <td><?php echo $dummy_data[3];?></td>
</tr>
<table>

In your example, in the excerpts where you have "echo", you would do something like this

    $ads[] = "<div style='width:720px;height:90px'>
  <a href='".$res["codigo"]."' target='_blank'><img src='img/anuncios/".$res["Nid"]."/".$res["arquivo"]."' height='90' width='720'> </a>
  </div>";

Then just print:

bla bla <?php echo $ads[0];?> codigo html qualquer<br />
<?php echo $ads[1];?> lorem ipsum, outro texto qualquer.

It's unclear how you want to control this, how many results you will return, and how you want to control where to print each of the results.

I think what you want to do is better with something like wordpress shortcodes.

It has a JavaScript library that simulates the same functionality: link

Example using shortcode.js

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">/script><scriptsrc="http://archive.nicinabox.com/shortcode.js/src/Shortcode.js"></script>

<table border="1">
<tr>
    <td>bla bla</td>
    <td colspan="2">[ads0][/ads0]</td>
</tr>
<tr>
    <td>lorem ipsum</td>
    <td>[ads1][/ads1]</td>
    <td>dumb lol</td>
</tr>
<tr>
    <td>foo bar</td>
    <td>[ads2][/ads2]</td>
    <td>[ads3][/ads3]</td>
</tr>
<table>



</body>
</html>


<?php
$dummy_data = array('anuncio a', 'anuncio b', 'anuncio c', 'anuncio d');
?>

<script type="text/javascript">
new Shortcode(document.querySelector('body'), {
<?php

/**
Aqui damos 20 voltas independente da quantidade de resultados provinda do banco de dados. O limite de 20 é baseado no script da pergunta onde o 'LIMIT' é 20.
*/

for ($i = 0; $i < 20; $i++) {
?>
    ads<?php echo $i;?>: function(done) {
        return '<?php echo (isset($dummy_data[$i])?$dummy_data[$i]:'');?>';
    },
<?php
}
?>
});
</script>
    
02.03.2016 / 19:58