This is a discussion on pulling data from array within the PHP General forums, part of the PHP Programming Forums category; I am trying to pass the data from a table to a google.visualization.OrgChart javascript I have converted the ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I am trying to pass the data from a table to a
google.visualization.OrgChart javascript I have converted the data to an array called $customf_array. Look down in the script to where I need to pull the data out of the array to see the reset of my question $query = "SELECT * FROM member WHERE members_id = '$_SESSION[members_id]'"; $result=mysql_query($query); $row = mysql_fetch_array($result); $memberrep=$row['repnumber']; $query1 = "SELECT sponserrep,rep,firstname,lastname,phonenumber FROM contacts WHERE members_id = '8' AND sponserrep != ''"; $result1=mysql_query($query1); $num_rows = mysql_num_rows($result1); while($row1=mysql_fetch_array($result1)){ $customf_array[]=$row1[0] . "," . $row1[1] . "," . $row1[2] . "," . $row1[3]; } foreach ($customf_array as $value) { echo "Value: $value<br />\n"; } this is the data that is put out by the foreach Value: 0000,1234,top,manager Value: 1342,4523,Leeann,Rodriguez Value: 4325,1111,Carolyn,Sanderssandifer Value: 1342,4567,Annie B,Scott Value: 1234,4325,Jessica,Hobirk Value: 1234,12356,Kaye,Meadows Value: 1234,1342,Misty,Coe Value: 1234,3462,Donald,Skinner ?> <html> <head> <script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("visualization", "1", {packages:["orgchart"]}); google.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.DataTable(); data.addColumn('string', 'Name'); data.addColumn('string', 'Manager'); data.addRows(<? echo $num_rows?>); data.setCell(0, 0, '<? echo$row["firstname"]?> <? echo$row["lastname"]?>','<font size="+1" color="#FF0000"><? echo$row["firstname"]?> <? echo$row["lastname"]?> </font><br><? echo$row["phonemember"]?>'); <*****************************look at this section***********************> This is what I do not understand how to do if you can give me some examples on how to sort and pull this data out of the array or if there is a better way to do this I have been working with this for a few days and do not no where to go This is the section I need to loop through the array and add the data it needs to number each recored and the add the members name according to the rep# and the managers name according to the sponserrep# $number //the recored number $addname // the members name $addmanager// the managers name data.setCell($number, 0, '$addname','<h1>$addname</h1>'); data.setCell($number, 1, '$addmanager'); <*****************************l******************* ****> var table = new google.visualization.OrgChart(document.getElementB yId('chart_div')); table.draw(data, {allowHtml:true}); } </script></head> <body><div id="chart_div"></div></body> </html> |
|
|||
|
> -----Original Message-----
> From: Richard Kurth [mailto:richardkurth@centurytel.net] > Sent: Monday, September 08, 2008 1:55 PM > To: PHP General List > Subject: [php] pulling data from array > > I am trying to pass the data from a table to a > google.visualization.OrgChart javascript > I have converted the data to an array called $customf_array. > Look down in the script to where I need to pull the data out of the > array to see the reset of my question ---8<--- snip! > <*****************************look at this > section***********************> > This is what I do not understand how to do if you can give me some > examples on how to sort and pull this data out of the array or if there > is a better way to do this > I have been working with this for a few days and do not no where to go > > This is the section I need to loop through the array and add the data > it needs to number each recored and the add the members name according > to the rep# and the managers name according to the sponserrep# > $number //the recored number > $addname // the members name > $addmanager// the managers name > > data.setCell($number, 0, '$addname','<h1>$addname</h1>'); > data.setCell($number, 1, '$addmanager'); > > > <*****************************l******************* ****> > > var table = new > google.visualization.OrgChart(document.getElementB yId('chart_div')); > table.draw(data, {allowHtml:true}); > } > > </script></head> > <body><div id="chart_div"></div></body> > </html> 1.) Sort your data in the SQL query, not with PHP or Javascript. Google for the "ORDER BY" SQL clause. 2.) Don't cram all the values into an array of strings that are joined with commas if you're only going to break them back apart and meddle with them individually (as you are). Just create a simple object/structure to hold the data you're working with (number, name, manager's name) and make an array of those. Here's an example: <?php class valueObject { var $id, $name, $manager; } # assign values to the objects and push them into an array like this: while($row = mysql_fetch_array($result)) { $obj = new valueObject(); $obj->id = $row['id']; $obj->name = $row['firstname'] . $row['lastname']; $obj->manager = $row['manager']; $objArray[] = $obj; } ?> <!-- start your javascript here --> <script type="text/javascript"> function drawChart() { // init blah blah blah <?php # now loop through your object array and translate to js function calls foreach($objArray as $record) { echo "data.setCell(0, 0, '{$row['firstname']}', '{$row['lastname']}');"; } ?> } </script> .... I hope that helps. Somehow, I don't think you're using the Google Apps Javascript function ".setCell" properly. I'm guessing the first to parameters are the cell coordinates, and it's always [0,0] in your loop. Anyway... Todd Boyd Web Programmer |
|
|||
|
> -----Original Message-----
> From: Boyd, Todd M. > Sent: Monday, September 08, 2008 2:28 PM > To: 'Richard Kurth'; 'php-general@lists.php.net' > Subject: RE: [php] pulling data from array ---8<--- snip > <?php > > # now loop through your object array and translate to js function calls > > foreach($objArray as $record) { > echo "data.setCell(0, 0, '{$row['firstname']}', > '{$row['lastname']}');"; > } > > ?> That should have been {$record['firstname']}, etc... not $row. My bad--it's been a long day. :P Todd Boyd Web Programmer |
|
|||
|
Richard Kurth wrote:
> I am trying to pass the data from a table to a > google.visualization.OrgChart javascript > I have converted the data to an array called $customf_array. > Look down in the script to where I need to pull the data out of the > array to see the reset of my question > > > $query = "SELECT * FROM member WHERE members_id = '$_SESSION[members_id]'"; > $result=mysql_query($query); > $row = mysql_fetch_array($result); > $memberrep=$row['repnumber']; > > > $query1 = "SELECT sponserrep,rep,firstname,lastname,phonenumber FROM > contacts WHERE members_id = '8' AND sponserrep != ''"; > $result1=mysql_query($query1); > $num_rows = mysql_num_rows($result1); > while($row1=mysql_fetch_array($result1)){ Change the previous line to this while ( $row1 = mysql_fetch_assoc($result1) ) { > $customf_array[]=$row1[0] . "," . $row1[1] . "," . $row1[2] . "," . > $row1[3]; Change the previous line to this $customf_array[] = $row1; > } > > > > foreach ($customf_array as $value) { > echo "Value: $value<br />\n"; > } > > this is the data that is put out by the foreach > Value: 0000,1234,top,manager > Value: 1342,4523,Leeann,Rodriguez > Value: 4325,1111,Carolyn,Sanderssandifer > Value: 1342,4567,Annie B,Scott > Value: 1234,4325,Jessica,Hobirk > Value: 1234,12356,Kaye,Meadows > Value: 1234,1342,Misty,Coe > Value: 1234,3462,Donald,Skinner > > > > ?> > <html> > <head> > <script type="text/javascript" src="http://www.google.com/jsapi"></script> > <script type="text/javascript"> > google.load("visualization", "1", {packages:["orgchart"]}); > google.setOnLoadCallback(drawChart); > function drawChart() { > var data = new google.visualization.DataTable(); > data.addColumn('string', 'Name'); > data.addColumn('string', 'Manager'); > data.addRows(<? echo $num_rows?>); > data.setCell(0, 0, '<? echo$row["firstname"]?> <? > echo$row["lastname"]?>','<font size="+1" color="#FF0000"><? > echo$row["firstname"]?> <? echo$row["lastname"]?> > </font><br><? echo$row["phonemember"]?>'); > > > > > > <*****************************look at this section***********************> > This is what I do not understand how to do if you can give me some > examples on how to sort and pull this data out of the array or if there > is a better way to do this > I have been working with this for a few days and do not no where to go > > This is the section I need to loop through the array and add the data > it needs to number each recored and the add the members name according > to the rep# and the managers name according to the sponserrep# > $number //the recored number > $addname // the members name > $addmanager// the managers name > data.setCell($number, 0, > '$addname','<h1>$addname</h1>'); data.setCell($number, 1, > '$addmanager'); > > > <*****************************l******************* ****> Having no knowledge of how the setCell() methods deals with input, this is my best guess at how it should go together. data.addRows(<? echo count($customf_array); ?>); foreach ( $customf_array AS $row_id => $row ) { foreach ( $row AS $cell_id => $value ) { $value = htmlspecialchars($value); echo "data.setCell({$row_id}, {$cell_id}, '{$value}', '<h1>{$value}</h1>');\n"; } } Hope this helps. > var table = new > google.visualization.OrgChart(document.getElementB yId('chart_div')); > table.draw(data, {allowHtml:true}); > } > > </script></head> > <body><div id="chart_div"></div></body> > </html> > -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare |
|
|||
|
> -----Original Message-----
> From: Jim Lucas [mailto:lists@cmsws.com] > Sent: Monday, September 08, 2008 2:38 PM > To: Richard Kurth > Cc: PHP General List > Subject: Re: [php] pulling data from array ---8<--- snip > > $query1 = "SELECT sponserrep,rep,firstname,lastname,phonenumber FROM > > contacts WHERE members_id = '8' AND sponserrep != ''"; > > $result1=mysql_query($query1); > > $num_rows = mysql_num_rows($result1); > > while($row1=mysql_fetch_array($result1)){ > > Change the previous line to this > > while ( $row1 = mysql_fetch_assoc($result1) ) { > > > $customf_array[]=$row1[0] . "," . $row1[1] . "," . $row1[2] . "," .. > > $row1[3]; > > Change the previous line to this > > $customf_array[] = $row1; Gahhh... I've been working with ASP.NET too much recently. I didn't even see that he wasn't fetching the row as an associative array. I seem to remember using mysql_fetch_row() for it when I was doing more PHP/DB-oriented coding. Object instantiation to contain a row of information that could already be accessed methodically and easily was a bit of overkill in my "solution." :) Cheers, Todd Boyd Web Programmer |