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

Asked

Viewed 76 times

0

Well I have a site where I am to each area using a select , I think I’m making more queries than I should, is there any way to repeat the result ? follows a picture of what I am proposing

inserir a descrição da imagem aqui

     <?php
     try {
     $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;
    }
    } ?>

1 answer

2


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

Take 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 passages where there is "echo", 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>";

So just print it out:

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

It is unclear how you want to control this, how much 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.

Has a Javascript library that simulates the same functionality: http://archive.nicinabox.com/shortcode.js/

Example using shortcode.js

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">/script>
<script src="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>
  • in this case then in each result would have to use the right switch? because I need to separate the type of each one before , it would not be possible to already take all this check and put the result inside another array?

  • From what I understood from the code you posted, the switch is necessary due to the type of banner. As it can be text, image or flash. This switch has to keep. But the focus in question is on how to print the ads between the text and html of the page, right?

  • Yes, I’ll ask another question by being more detailed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.