Query result via ajax.

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
leonardorocha
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Fri Mar 28, 2014 7:29 pm

Query result via ajax.

Post by leonardorocha » Sun Jul 21, 2019 1:34 am

I'm creating a module to bring results of a search via ajax.

My helper looks like this:

Code: Select all

if(!empty($idDirigente)) {
    
    $input = JFactory::getApplication()->input;
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);

    $output = '';

        $query->select($db->quoteName('*'));
        $query->from($db->quoteName('#__agendadirigentes_compromissos'));

    $db->setQuery($query);
  
    $result = $db->loadObjectList();
    $output = null;
    if($db->getNumRows(); > 0) {
      foreach( $result as $res ) {
        $output .= '
        <ul class="list-compromissos">
            <li class="item-compromisso-wrapper">
    	        <div class="item-compromisso">
    	          	<div class="compromisso-horarios">
    	          		<time class="horario compromisso-inicio" datetime="2019-06-10 08h50">'. $res["horario"] .'</time>
    	          	</div>
    	          	<div class="compromisso-dados">
    		          <h4 class="compromisso-titulo">'. $res["title"] .'</h4>
                  <p class="compromisso-local">'. $res["local"] .'</p>
    		          <a class="add-agenda vcal" href="index.php?option=com_agendadirigentes&view=compromisso&format=vcs&id='. $res["ac.id"] .'">VCAL</a>
    		          	<span>Adicionar ao meu calendario</span>
    		          </span>
    		        </div>
    	        </div>            
            </li>
        </ul>';  
      }  
    }
    if($output == null or empty($data))
        {
            $output = 'Sorry! No results for your search.';
        }
    echo $output;  
  } 
But there is no result.

With the helper below the result is presented without problems, however I need it to be like the above helper

Code: Select all

      if (!empty($buscaAgenda)){
        $query .= "OR ac.title LIKE '%$buscaAgenda%' AND ad.id = '$idDirigente'
        ORDER BY ac.id DESC";
      } 
      $result = mysqli_query($connect, $query);
      if(mysqli_num_rows($result) > 0)
      {
        while($row = mysqli_fetch_array($result))
        {
        $output .= '
        <ul class="list-compromissos">
		        <li class="item-compromisso-wrapper">
			        <div class="item-compromisso">
			          	<div class="compromisso-horarios">
			          		<time class="horario compromisso-inicio" datetime="2019-06-10 08h50">'. $row["horario"] .'</time>
			          	</div>
			          	<div class="compromisso-dados">
				          <h4 class="compromisso-titulo">'. $row["title"] .'</h4>
                  <p class="compromisso-local">'. $row["local"] .'</p>
                  <p class="compromisso-local">'. $row["data_inicial"] .'</p>
                  <p class="compromisso-local">'. $row["data_final"] .'</p>
                  <p class="compromisso-local">'. $row["id"] .'</p>
                  <p class="compromisso-local">'. $idDirigente .'</p>
				          <a class="add-agenda vcal" href="index.php?option=com_agendadirigentes&view=compromisso&format=vcs&id='. $row["ac.id"] .'">VCAL</a>
				          	<span>Adicionar ao meu calendario</span>
				          </span>
				        </div>
			        </div>
                            
		        </li>
        </ul>';  
        }  
    	} else {  
    	$output .= '  
        <div class="alert alert-dark" role="alert">
          	Termo <b>'.$buscaAgenda.'</b> não encontrado!
        </div>';  
    	}  
      	$output .= '</div>';  
      	echo $output;  
    } 
Someone, please, have a solution? Because I've been working on it for days and I can not solve it?

harperlala
Joomla! Guru
Joomla! Guru
Posts: 587
Joined: Mon Aug 24, 2015 5:55 pm

Re: Query result via ajax.

Post by harperlala » Sun Jul 21, 2019 4:31 am

do you have result of data? you must have this first, then:
- check ajax response
- add one div to get this data with jquery example

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 25879
Joined: Mon Oct 27, 2008 9:27 pm
Location: Akershus, Norway

Re: Query result via ajax.

Post by Per Yngve Berg » Sun Jul 21, 2019 8:35 am

Mod. Note: Relocating the topic to the Coding Forum.

https://docs.joomla.org/Using_Joomla_Ajax_Interface

leonardorocha
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Fri Mar 28, 2014 7:29 pm

Re: Query result via ajax.

Post by leonardorocha » Sun Jul 21, 2019 2:10 pm

Honestly, I did not understand what they reported.

If I have one code that works and the other does not, then I'd like to understand why this happens. The biggest problem is that I can not connect to the "$ db = JFactory :: getDbo ()" in the second code, if that is possible it solves the problem. Because this code already works 100%.

nickywoolf
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Fri Jul 26, 2019 1:08 pm

Re: Query result via ajax.

Post by nickywoolf » Fri Jul 26, 2019 1:18 pm

In your first code snippet you have this:

Code: Select all

if ($db->getNumRows(); > 0) {
    // ...
}
You can't have ";" after $db->getNumRows(). I'd expect to see: PHP Parse error: syntax error, unexpected ';' when you run that code.

Should be:

Code: Select all

if ($db->getNumRows() > 0) {
    // ...
}


Post Reply

Return to “Joomla! 3.x Coding”