adding dynamic rows to google chart - php

I'm using google Line chart for my college project, here i want to add rows dynamically based on the user selection from database, addRow() function is used to add a row but i want it to be add by looping. someone can help me?
Here is my code:
var rowArray1 = [];
var rowArray2 = [];
<?php
for($i=1;$i<=$count;$i++)
{
$row=mysql_fetch_array($rows);
echo "rowArray1.push('". $row['a'] ."')";
echo "rowArray2.push(". $row['b'].")";
array_push($rowArray,"'".$row['a']."',".$row['b']);
}
?>
for(i=0;i<count;i++)
{
data.addRow( [rowArray1[i], rowArray2[i]] );
}
it's not working properly... :-(

Finally I got the solution. it's very simple way. when i asked this question, i was just a beginner so i dont know how to do it. now i got the answer.
I just included the PHP scripts inside the javascript code like this,
<script>
// Google chart codes....
<?php
$row=mysql_fetch_array($rows);
foreach($row as $data) {
echo "data.addRow( $data['a'] , $data['b'] );";
}
?>
// Google chart codes....
</script>
It's worked fine... :)

Related

PHP and jQuery removing dynamic list item

I hope someone will be able to shed some light on why I am having this issue. I have created a basic list that is generated from a MySQL database using a PHP class. I am then trying to use some basic jQuery to remove a list item. I could not get it working for a long time eventually finding that if I moved the jQuery include to the end of the HTML file it works but not with it included at the top of the script as I would normally aim to do. I had thought that the jQuery document ready function should have resolved this but it is not for some reason.
Here is the jQuery (very basic as said):
$(document).ready(function(){
$(".completeButton").click(function(){
$(this).closest('li').remove();
});
});
Here is the HTML page body (notice I have the jQuery include near the end rather than in the header where it does not work and remove the item, it works fine with it here though?):
<?php
session_start();
if(isset($_SESSION['user'])) {
include "header.php";
?>
<h2>Task List</h2>
<?php
include_once "DatabaseConn.php";
include_once "taskList.class.php";
$sql = "SELECT `GV_Employee`.`requests`.`req_ID` , `GV_Employee`.`equipment`.`description` , `GV_Employee`.`employees`.`first_name`, `GV_Employee`.`employees`.`last_name`
FROM requests
RIGHT JOIN equipment
ON requests.eq_ID=equipment.eq_ID
RIGHT JOIN employees
ON requests.emp_ID=employees.emp_ID
ORDER BY requests.req_ID;";
$stmt = $db->prepare($sql);
$stmt->execute();
$results = $stmt->fetchALL();
foreach ($results as $row){
$task[] = new tasklist($row);
}
foreach($task as $item){
echo $item;
}
?>
<script src="tasklist.js"></script>
<?php
}else {
session_destroy();
header("Location: http://dunc2/dylanr/Starters&Leavers/index.php");
//header("Location: http://192.168.0.4/Starters&Leavers/index.php");
exit;
}
?>
Here is the class for the dynamic list:
<?php
class taskList {
private $taskData;
public function __construct($task){
if(is_array($task))
$this->taskData = $task;
}
public function __toString(){
return '<li id="task-'.$this->taskData['req_ID'].'" class="taskList">
<div class="text">'.$this->taskData['first_name'].' '.$this->taskData['last_name'].
' requires: '.$this->taskData['description'].'</div>
<div id="button">
Complete
Priority
</div>
</li>';
}
}
?>
I hope this question makes sense, I tried searching the forum but could not find a topic which covered this exact situation apologies if there is one.
Many thanks
James

multiple markers shows in google map in php codeigniter

How to execute the php foreach loop in java script? i need to loop latitude_code & longitude_code in java script
this line should be in loop
var centerLatLng = new google.maps.LatLng(latitudes,longitudes);
please suggest me..
thanks for advance..
in your view;
<script>
// Your Java...
<?php foreach($items as $item) { ?>
google.maps.LatLng(<?php echo $item->latitude; ?>,<?php echo $item->longitude; ?>);
<?php } ?>
</script>
Here's [demo](http://jsfiddle.net/2crQ7/),I think it will help you, in this example java script array is used and you are using php array.Just use your array for looping, don't pass location in setMarkers(map,locations) function .
Try with this example if you don't get answer I will provide my code.

how to get a PHP script to make javascript in li elements

My PHP script queries a database and returns the rows as li elements to a div in a jQuery Dialog. It does this by building an array in the PHP while loop that processes the query row responses. Each li element has some data, an HTML button, and some javascript to execute on that li element when the button is clicked.
If I write "bad HTML" with the same id for each li element, the javascript executes, but only on the first li element. When I write "good HTML" with a unique id for each li element, I can't get the javascript to execute. I've researched and tried lots of things, but can't find anything this complicated to fix this. What should I do? Here's the stripped down code. The problem seems to be in the lengthy $error_ListActives line, but I'm open to suggestions.
$rowCount = 0;
while ($row = mysql_fetch_array($fetch)) {
$storedStreetAddress = $row["streetAddress"];
$storedCity = $row['city'];
$error_NumberOfActives = "<li>Welcome back. You have . . .</li>";
$errorMessages[0] = $error_NumberOfActives;
$idMaker = "inactive" . $rowCount;
$error_ListActives = "<li> $storedStreetAddress, $storedCity $idMaker
<button type='button' id=$idMaker onclick='makeInactive()'>Pause this</button>
<script type='text/javascript'>function makeInactive()
//do stuff
{document.getElementById($idMaker).innerHTML='Inactive Completed';}
</script> </li>";
$errorMessages[] = $error_ListActives;
$rowCount++;
}
foreach( $errorMessages as $statusMessage ) {
echo $statusMessage;
}
Please be specific, and code is helpful. I don't follow general instructions well in this area, as I'm a newbie with PHP.
You're going to end up with a lot of problems here because you are writing your javascript function inside a while loop. That means that you will get several copies of the same function, and only the last one is going to actually fire!
To fix this, write a generic javascript function for makeInactive(id) and then just call it with the id of the element you are working on.
Outside of the php loop:
<script type='text/javascript'>function makeInactive(id)
//do stuff
{document.getElementById(id).innerHTML='Inactive Completed';}
</script>
Then change the php loop to:
while ($row = mysql_fetch_array($fetch)) {
$storedStreetAddress = $row["streetAddress"];
$storedCity = $row['city'];
$error_NumberOfActives = "<li>Welcome back. You have . . .</li>";
$errorMessages[0] = $error_NumberOfActives;
$idMaker = "inactive" . $rowCount;
$error_ListActives = "<li> $storedStreetAddress, $storedCity $idMaker
<button type='button' id='$idMaker' onclick='makeInactive($idMaker)'>Pause this</button>
</li>";
$errorMessages[] = $error_ListActives;
$rowCount++;
}
Why not:
<button onClick="makeInactive(<?php echo $row['id'] ?>);">...</button>
embed the ID code as a parameter to your MakeInactive() call. Other alternatives are to simply use some DOM, e.g.
<li id="xxx"><button onclick="makeInactive(this);">...</button></li>
<script>
function makeInactive(el) {
el.getParent('li').disable(); // not real code, but shows the basic idea
}
</script>
First of all, you're creating the same exact function multiple times on your page. Your other buttons don't know which makeInactive() function to use. Why don't you give all of your buttons the same class, and then create ONE function that is included on the page that fires when the buttons are clicked.
No offense, but this is kind of bad markup here.

Moving JavaScript into PHP

I'm just starting out with PHP, and I am attempting to move some jQuery ajax into PHP. Here is my PHP file:
<?php
include 'config.php';
include 'opendb.php';
$query = "SELECT * FROM agency ORDER BY name";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
$id = $row['id'];
$name = $row['name'];
echo "<li class=\"agency\">$name<ul class=\"agency-sub\"></ul></li>";
}
include 'closedb.php';
?>
Here is my current js function:
//Add Agency content
$("ul.top-level").on("click", "li.agency a", function (event) {
if($(this).next().length) {
var numbs = $(this).attr("href").match(/id=([0-9]+)/)[1];
showContentAgency(numbs, this);
} else {
$(this).closest('ul').find('a').removeClass('sub-active');
}
event.preventDefault();
});
And here is the showContentAgency(); function:
function showContentAgency(id, elem) {
$.post("assets/includes/contentAgency.php?id=id", {
id: id
}, function (data) {
$(elem).addClass("nav-active").parent().find("ul").html(data).show();
});
}
What I'd like to do is have PHP render the unordered list rather than have jQuery insert it. This is how it is currently featured in the above PHP file:
echo "<li class=\"agency\">$name<ul class=\"agency-sub\"></ul></li>"
So I would like the PHP to populate the <ul class="agency-sub"> list.
The structure of your code is a little bit unclear to me, but the broad outline is this: You take whatever function is generating the content in contentAgency.php and call that to get the HTML, then stick that inside when you're building up the list.
Php can not access the DOM of the page like Jquery can. If you wanted to access the DOM with Php, you would have to parse the entire web page, which is probably impractical for what you want to do. Php can only modify the page before it is loaded by the browser. If you want to run code after page load, you have to use javascript.
We might be able to help you more if you post more of your code, as we currently don't know what the page's code looks like.
Is this a list inside list?
By the way you can write php code like below, it is more readable
<?php
include 'config.php';
include 'opendb.php';
$query = "SELECT * FROM agency ORDER BY name";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
$id = $row['id'];
$name = $row['name'];
echo "<li class='agency'><a href='contentAgency.php?id=$id'>$name</a><ul class='agency-sub'></ul></li>";
}
include 'closedb.php';
?>
if you are using double quotes in echo you can use single quotes inside.
I'm uncertain as to what exactly you want but it sounds like you're looking for the 'foreach' loop. When I wanna populate a list of stuff from a result set i simple use:
<ul>
<? foreach($result as $object) ?>
<li><?=$object?></li>
<? endforeach; ?>
</ul>
foreach acts a for loop but doing all the logic in the background. Hope this helps.

Magento Configurable Product Attributes

hey guys, having an issue with magento which i just cant seem to find a solution to.
i have tried many ways of getting a configurable products attributes (simple products) and listing them, now i have them listing from 2 ways but the way im working with is below
$confAttributes = #$_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
$sizes = array();
foreach($confAttributes AS $atts){
//print '';//'<pre style="display:none;">'.print_r($atts).'</pre>';
if($atts['label'] == 'Size'){
foreach($atts['values'] AS $val){
$sizes[] = $val['store_label'];
}
}
}
my only problem with this now is i need to only pull back the size attributes which are in stock - looked through mage files to find solution but just cant see anything - the result i need is done in config product php file but i cant access it from in the code where i need to list the size attribute.
any help would be great, thanks!
Solution:
You can get easily all configurable(product) details page information on any other PHTML file by using following code:
e.g.: in my case i'm getting details on catalog/product/list.phtml.
<script src="<?php echo Mage::getBaseUrl('js') ?>varien/configurable.js" type="text/javascript"></script>
<?php
$temp = new Mage_Catalog_Block_Product_View_Type_Configurable();
$temp->setData('product', $_product);
$_attributes = Mage::helper('core')->decorateArray($temp->getAllowAttributes());
?>
<?php if ($_product->isSaleable() && count($_attributes)):?>
<?php foreach($_attributes as $_attribute): ?>
<?php
$prices = $_attribute->getPrices();
foreach($prices as $price) {
echo $price['pricing_value'] . "<br/>";
}
?>
<?php endforeach; ?>
<script type="text/javascript">
var spConfig = new Product.Config(<?php echo $temp->getJsonConfig() ?>);
</script>
<?php endif;?>
Thanks,
found the solution, i had to use the above what i had already coded and use assosicated products for the size and then check the stock levels and put them into an array and check the stock when building my attribute list - works great - anyone else have a better solution please share :D thanks

Categories