Jump to content

mysql php and json


lee1963

Recommended Posts

help required for noobie(no hair left)

all i want to be able to do is use the individual elements of the array in javascript eg companyname or companyimage

but just getting my head in my hand.

 

mysql_select_db("herefordinfo") or die(mysql_error());

echo "Connected to Database";

 

$dbQuery = 'SELECT * FROM  companies';

$dbResult = mysql_query($dbQuery) or die ("Could not read data because " . mysql_error());

$RowCount = mysql_numrows($dbResult);

 

while ($pubs = mysql_fetch_assoc($dbResult))

{

$array[] = "{  {$pubs['id']}, {$pubs['companyname']}, {$pubs['companyinfo']},  {$pubs['companyimage']} }";

 

}

echo 'var pub = [' . implode(', ', $array) . '];'; // this

 

?>

<html>

<head>

 

<script type="text/javascript">

var jsArray = <?php echo json_encode($array); ?>; 

document.write(jsArray); //this

</script>

</head>

<boby></body>

</html>

 

both php and javescritp output the results below

{ 1, lichfield vaults, 10 church street hereford , lich.jpg },{ 2, Black Lion, bridge street, blacklion.jpg },{ 3, Saracens Head, st martins street, saracens.jpg }

Link to comment
Share on other sites

You're over-thinking this, try something like this:

<?php
$dbQuery = 'SELECT * FROM  companies';
$dbResult = mysql_query($dbQuery) or die ("Could not read data because " . mysql_error());
$RowCount = mysql_numrows($dbResult);

while ($pubs = mysql_fetch_assoc($dbResult))
{
$array[] = array('id'=>$pubs['id'],'companyname'=>$pubs['companyname'],'companyinfo'=>$pubs['companyinfo'],companyimage=>$pubs['companyimage']);
  
}
echo '<pre>' . print_r($array,true) . '</pre>';
echo '<pre>' . json_encode($array) . '</pre>';
?>
<html>
<head>

<script type="text/javascript">
var jsArray = <?php echo json_encode($array); ?>;  
document.write(jsArray[0].companyname); //this
</script>
</head>
<boby></body>
</html>

 

Ken

Link to comment
Share on other sites

firstly thx for response , your right i do have a tendancy to overly complicate things think its because im still new to this. but slowly getting the hang of it.

could you give me some snippet of code that will help me with javascript side. think then i can get on , thx in advance

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.