简体   繁体   中英

PHP array with MySql data into JS file in Joomla

I'm trying to make a calendar with birthdays in Joomla and I have this code :

include '../../../../connect.php';
$sql="select * from intranetUsers"; 
$result = ($db->setQuery($sql));
$USER_hbd = array();
$USER_shortName = array();

if( $rows = $db->loadObjectList() )
{
  foreach( $rows as $row )
  {
      $USER_hbd[] = $row->USER_hbd;
      $USER_shortName[] = $row->USER_shortName;
      echo  "var Events  = '".$USER_hbd[] = $row->USER_hbd."':"."'<a href='#'><span>'".$USER_shortName[] = $row->USER_shortName."</span></a>'";
  }
}

And I would like get this

var Events = {
    '01-02-2018' : '<a href="#">'+'Luz Naranjo<span>(Campo Pereira)</span></a>',
    '01-09-2018' : '<a href="#">'+'Martha Leguizamon<span>(Campo Armenia)</span></a>'+
                   '<a href="#"> Stefania Rosso<span>(Comercial Medellín)</span></a>',
    '01-28-2018' : '<a href="#">'+'Lorena Cortes<span>(Campo Barranquilla)</span></a>'
};

But if put the echo inside the foreach I got this:

var Events = '01-02-2018':'Luz Naranjo'
var Events = '01-09-2018':'Martha Leguizamon'
var Events = '01-09-2018':'Stefania Rosso'
var Events = '01-28-2018':'Lorena Cortes'

And if I put the echo after the if i got just the last result

var Events = '01-28-2018':'Lorena Cortes'

I don't know how just have one var Events with all the info and notice that in some cases are two people to the same date(birthday). Hope you can help me, thanks.

you should save the php data, to a associative array, and then use json_encode

$events = array();
if( $rows = $db->loadObjectList() )
{
  foreach( $rows as $row )
  {
    $events[$row->USER_hbd][] = $row->USER_shortName;
  }
}
echo json_encode($events);

The output won't be exaclty as you want, but it will more useful than what you have now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM