PHP functions in ajax

0

Good, I'm with a little drama in my front end. Well, I'm wanting to show with all the records of a table, what happens is that two fields are ID of another table, but when I insert inside the concatenation the function to make the change of text by ID, does not reflect anything to me, in pure PHP if it does, but when it is calling by AJAX, nothing appears. I accompany the code. Can someone give me a hand?

All records retrieved from the database are displayed here.

            <div id="resultado-registro">
                <?php include('destacados.php'); ?>
            </div>

And here where they are prepared (only I put the final part, if necessary more info, they warn me).

$output = '';
if(!empty($faq)) {
$output .= '<input type="hidden" class="pagenum" value="' . $page . '" /><input type="hidden" class="total-page" value="' . $pages . '" />';
foreach($faq as $k=>$v) {
    $output .= '

    <div data-id="'.$faq[$k]["id"].'" class="item item'.$faq[$k]["id"].' image-present align-center item-featured reviews-enabled">
                        <a href="imagenes/servicios/'.$faq[$k]["imagen"].'" target="_blank" rel="item-gallery" class="cboxElement">
                        <div class="item-thumbnail">
                            <div class="item-thumbnail-wrap">
                                <img src="imagenes/servicios/'.$faq[$k]["imagen"].'" alt="" class="imagen" alt="'.$faq[$k]["nombre"].'">
                            </div>
                            <div class="item-text">
                                <div class="item-excerpt">
                                    <p>
                                        '.$faq[$k]["resena"].'
                                    </p>
                                </div>
                            </div>
                        </div>
                        <div class="item-title">
                            <h3>'.$faq[$k]["nombre"].'</h3>
                        </div>
                        <div class="item-categories">
                            <span class="item-category">'.$faq[$k]["rubro_id"].'</span>
                            <span class="item-category">'.$faq[$k]["localidad_id"].'</span>
                        </div>
                        </a>
                        <div class="review-stars-container">
                            <div class="content">
                                <a href="servicio.php?id='.$faq[$k]["id"].'">VER</a>
                            </div>
                        </div>
                    </div>
        ';
}
}
print $output;

As you can see, where it says RUBRO_ID and LOCALIDAD_ID is where I want to apply the function so that the other table, retrieve the name of each item and location according to your ID.

    
asked by Rodrigo Ruiz Diaz 26.09.2016 в 03:41
source

1 answer

0

You could search and return the item and the locality in the same query you do when bringing the products.

1 .- Assuming the product / category and product / location relationship is 1: 1 , you can, for example, make use of the JOIN function :

<?php
$query = 'SELECT P.*,
  R.nombre AS nombreRubro,
  L.nombre AS nombreLocalidad
FROM productos AS P
INNER JOIN rubros AS R
  ON R.id = P.rubro_id
INNER JOIN localidades AS L
  ON L.id = P.localidad_id';

2 .- If the relationship was 1: N (a product has more than one item or location), you could search all the items and locations previously, save them in a matrix where the index is the Id and then refer to it:

<?php
// Vale la pena destacar que si son muchos los rubros y localidades
// esto puede ser poco eficiente
<span class="item-category">'.$rubros[$faq[$k]["rubro_id"]].'</span>
<span class="item-category">'.$localidades[$faq[$k]["localidad_id"]].'</span>
    
answered by 12.10.2016 в 00:57