Hello I have the following php script
$db =& JFactory::getDBO();
$sql = "select uid from #__zcalendar_events where article_id = " . $article->id;
$db->setQuery($sql);
$results = $db->loadObjectList();
if(count($results)) {
foreach($results as $r => $value) {
$sql = "select catid from #__zcalendar_event_categories where uid = " . $r->field1;
print_r($results);
print_r($sql);
My problem start in this line:
$sql = "select catid from #__zcalendar_event_categories where uid = " . $r->field1;
the . $r->field1; does not add the value to the string $sql.
But if I print_r($results); I get the this information:
Array ( [0] => stdClass Object ( [uid] => 2011-08-22-12-24-35-62-0#cttcorp.hexasystems.com ) )
And I need just this: 2011-08-22-12-24-35-62-0#cttcorp.hexasystems.com
and . $r->field1; have to had this information but it's always empty.
Can someone please help me to solve this problem, because I have the information. But never works.
A foreach is usually like this:
foreach($array as $key => $element)
Are you sure you want to be using the $r not the $value?
Related
Hi guys i would like to ask help on my problem..
I have 4 variables made from my query:
$sql1 = mysql_num_rows(mysql_query("SELECT * FROM tbl_reservation WHERE YEAR(date_added) = YEAR(NOW()) AND order_action = 'Online Transaction' "));
$sql2 = mysql_num_rows(mysql_query("SELECT * FROM tbl_reservation WHERE YEAR(date_added) = YEAR(NOW()) AND order_action = 'Walkin Transaction' "));
$sql3 = mysql_num_rows(mysql_query("SELECT * FROM tbl_reservation WHERE YEAR(date_added) = YEAR(NOW())-1 AND order_action = 'Online Transaction' "));
$sql4 = mysql_num_rows(mysql_query("SELECT * FROM tbl_reservation WHERE YEAR(date_added) = YEAR(NOW())-1 AND order_action = 'Walkin Transaction' "));
I would like to put them in an array..to be exactly on my expectations.
Here is my sample code:
$stack = array($sql1, $sql2);
array_push($stack, $sql3, $sql4);
$query = print_r($stack);
$result = $query;
print json_encode($result);
It display like this on my browser:
Array ( [0] => 0 [1] => 8 [2] => 0 [3] => 1 ) true
But i want to display it like this:
[{"0","1"},
{"2","3"}]
I am planning to make a line graph on this.. I have a Chart.min.js and jquery.min.js
If any way.. The whole point.. i want to make a line graph that would compare my data from current year to last year.
Help me pls. :(
Give a try
<?php
$a = array(0,8,0,1) ;
echo "<pre>";print_r(json_encode(array_chunk($a, 2)));
?>
with that. You can get this result
[[0,8],[0,1]]
Btw, Why you need to do that with your array ? .
Edited
After i check your question again, So you want get the order
<?php
$stack = array(0,8,0,1) ;
$array2= array();
foreach ($stack as $key => $val) {
$array2[] = $key;
}
echo "<pre>";print_r(json_encode(array_chunk($array2, 2)));
?>
I have this code
$query = "SELECT `subscriberid`,`data` FROM `****table_name*****`"
. "WHERE `subscriberid` IN (123,456,789,101)";
$result = $cxn->query($query);
$Points = array();
while ($row = $result->fetch_assoc()) {
$Points[$row['subscriberid']] = $row['data'];
}
I want the keys for $Points to be the subscriberid, but when I print out $Points I keep getting default keys 0-3 and I can't see any reason for this to be happening.
Credits to #Jongosi's Comment, about the if ($result = $cxn->query($query)) part.
Your query is currently as follows:
$query = "SELECT `subscriberid`,`data` FROM `****table_name*****`"
. "WHERE `subscriberid` IN (123,456,789,101)";
If you only edited ****table_name*****, you are missing a space ( between * and WHERE).
Your result will be nothing or an error.
$query = "SELECT `subscriberid`,`data` FROM `****table_name*****`"
. " WHERE `subscriberid` IN (123,456,789,101)";
I am trying to Query a MySQL table to to bring any result that matches data that the user has input. The database,table and column names are also dynamically stored in variables. var_dump produces a bool(false) which means my query is wrong.
My Code
if (isset ( $_POST ['name'] )) {
$name = trim ( $_POST ['name'] );
$tblName = $_REQUEST ['tbl'];
$colqry = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$dbName' AND TABLE_NAME = '$tblName'";
echo "<ul>";
$col_names = mysqli_query ( $link, $colqry );
while ( $col = mysqli_fetch_array ( $col_names, MYSQL_ASSOC ) ) {
$colName = $col ['COLUMN_NAME'];
$tblQry = "SELECT * FROM $tblName WHERE $colName=$name LIMIT 10";
$query2 = mysqli_query ($link, $tblQry);
echo $query2;
while ( $query3 = mysqli_fetch_array ( $query2 ) ) {
echo "<li onclick=fill'" . $query3 [0] . "'>" . $query3 [0] . "</li>";
}
}
}
What I want to achieve is list a table where the search terms matches something on the table either the column name or the data inside the columns
This line:
$tblQry = "SELECT * FROM $tblName WHERE $colName=$name LIMIT 10";
Quote the $name variable:
so it reads as WHERE $colName='$name'
You can then use $query3[$colname] to get the search match you're looking for.
For more information on identifer qualifiers, visit:
http://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html
I have this function:
function findAllmessageSender(){
$all_from = mysql_query("SELECT DISTINCT `from_id` FROM chat");
$names = array();
while ($row = mysql_fetch_array($all_from)) {
$names[] = $row[0];
}
return($names);
}
that returns all the ID of my users in a private messaging system. Then I want to get the all the messages where the user_id is equal to the user logged in and from_id is equal to all from_id I got from the previous function:
function fetchAllMessages($user_id){
$from_id = array();
$from_id = findAllmessageSender();
$data = '\'' . implode('\', \'', $from_id) . '\'';
//if I echo out $ data I get these numbers '113', '141', '109', '111' and that's what I want
$q=array();
$q = mysql_query("SELECT * FROM chat WHERE `to_id` = '$user_id' AND `from_id` IN($data)") or die(mysql_error());
$try = mysql_fetch_assoc($q);
print_r($try);
}
print_r return only 1 result:
Array (
[id] => 3505
[from_id] => 111
[to_id] => 109
[message] => how are you?
[sent] => 1343109753
[recd] => 1
[system_message] => no
)
But there should be 4 messages.
You have to call mysql_fetch_assoc() for each row that is returned. If you just call mysql_fetch_assoc() once then its only going to return the first row.
Try something like this:
$result = mysql_query("SELECT * FROM chat WHERE `to_id` = '$user_id' AND `from_id` IN($data)") or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
'mysql_fetch_assoc' returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead.
You need to iterate array like:
while ($row = mysql_fetch_assoc($q)) {
echo $row["message"];
}
I'm having a problem running a sql query using php.
$sql = "SELECT * FROM ".self::$table_name;
$result = mysql_query( $sql );
$r = mysql_fetch_array( $result );
print_r( $r );
die( '<br>'.$sql );
I have around 70 records in table but i'm only getting the first record.
see example.
Array ( [0] => site_url [setting_name] => site_url [1] => http://domain.com [value] => http://domain.com )
SELECT * FROM siteconfig
When I run the query in phpmyadmin. it works fine.
You have to make a loop to grab all the results:
$r = array();
while($junk = mysql_fetch_array($result)) $r[] = $junk;
print_r($r);
Do it like below:
$sql = "SELECT * FROM ".self::$table_name;
$result = mysql_query( $sql );
while($r = mysql_fetch_array($result)){
echo $r['col1']. " - ". $r['col2'];
// your stuff
}