I have the function:
function get_current_users($current_users)
{
global $db;
$current_users = $db->EscapeString($current_users);
$total_current_users = $db->QueryFetchArray("SELECT COUNT(*) FROM users AS total_current_users");
return $total_current_users['total_current_users'];
}
But I have no clue how to output the result, I've tried the following options but nothing displays:
`<?$total_current_users?>
<?['$total_current_users']?>
<?=data['$total_current_users']?>`
If you are certain that your function does what it's supposed to, I guess the two equivalent methods would gain you the output:
<?= get_current_users() ?> or
<?php echo get_current_users() ?>
As a note: I don't see the point of your argument $current_users that you escape -- because it never gets use in the subsequent query.
Try this syntax
$total_current_users = $db->QueryFetchArray("SELECT COUNT(*) as total_current_users FROM users");
You are selecting one value from inside the result set to return, this value isn't named, try the above to get it to match your code.
then change the template code to be: <?php echo $total_current_users; ?>
Related
I'm fairly new to PHP, and i have a problem in defining a function that returns an array containing a price and description strings.
I am using the "simple html dom" php files that facilitates parsing.
The function i create requires 2 arguments : the link (from which it will grab data) and the id (used to get the proper css syntax).
This is the get_product_details.php
<?
require_once 'simple_html_dom.php';
$priceMatchTable=('span[id=our_price_display]');
$descMatchTable=('div[id=short_description_content]');
function get_prod_details( $link , $id ) {
global $priceMatchTable, $descMatchTable;
$html = file_get_html($link);
$result['price'] = $html->find($priceMatchTable[$id],0);
$result['desc'] = $html->find($descMatchTable[$id],0);
return $result;
}
And this is the main php:
<?php
include 'get_product_details.php';
$link = 'http://micromedia.tn/barette-memoire/1170-barette-m%C3%A9moire-1go-ddr-ii.html';
$id = 0;
$result = get_prod_details($link, $id);
echo $result['price'];
?>
Finally i get an error which tell:
find($priceMatchTable[$id],0); $result['desc'] = $html->find($descMatchTable[$id],0); return $result; }
Fatal error: Call to undefined function get_prod_details() in C:\xampp\htdocs\dom\index.php on line 8
Best regards!
This may sound silliy, but is
include 'get_product_details.php';
really pointing towards "get_product_details.php"?
Disable (//) the function call in you index.php and add a simple echo to your "get_product_details.php" to see if the file gets included.
I think you need something like:
include '/path/from/root_to_your/directory/get_product_details.php';
If your trying this in Windows land, it will look something like:
include 'C:\Documents\something\get_product_details.php';
I have this code for scraping team names from a table
$url = 'http://fantasy.premierleague.com/my-leagues/303/standings/';
$html = #file_get_html($url);
//Cut out the table
$FullTable = $html->find('table[class=ismStandingsTable]',0);
//get the text from the 3rd cell in the row
$teamname = $FullTable->find('td',2)->innertext;
echo $teamname;
This much works.. and gives this output....
Why Always Me?
But when I add these lines..
$teamdetails = $teamname->find('a')->href;
echo $teamdetails;
I get completely blank output.
Any idea why? I am trying to get the /entry/110291/event-history/33/ as one variable, and the Why Always Me? as another.
Instead do this:
$tdhtml = DOMDocument::loadHTML($teamdetails);
$link = $tdhtml->getElementsByTagName('a');
$url = $link->item(0)->attributes->getNamedItem('href')->nodeValue;
$teamdetails = $teamname->find('a')->href;
^^^^^^^^^---- never defined in your code
I also fail to see how your "works" code could possibly work. You don't define $teamname in there either, so all you'd never get is the output of a null/undefined variable, which is...no output all.
Marc B is right, I get that you don't have to initialize a variable, but he is saying you are trying to access a property of said variable:
$teamdetails = $teamname->find('a')->href;
^^^^^^^^^---- never defined in your code
This is essentially:
$teamname = null;
$teamname->find('a')->href;
The problem in your example is that $teamname is a string and you're treating it like a simple_html_dom_node
There are two columns in the database table "system". I have the systemId and want to get the mobileSystemId. But the variable $mobileSystemIds which I already defined as global is always empty.
EDIT: Now array_map doesn´t work. I always get my Exception output "Arrayfehler ArrayMap"
I have the following code :
$mobileSystemIds=array();
function getMobileSystemId($systemId)
{
global $mysqli;
global $mobileSystemIds;
$query="SELECT mobileSystemId FROM system WHERE systemId ='" .$systemId ."'";
if(!$result=$mysqli->query($query))
{
echo "Datenbankfehler DB-QUery";
exit(0);
}
if (!$mobileSystemId=$result->fetch_assoc())
{
echo "Datenbankfehler DB-Fetch";
exit(0);
}
$mobileSystemId=$mobileSystemId["mobileSystemId"];
echo "mobile System ID: " .$mobileSystemId ."<br />";
return $mobileSystemId;
}
if(!$mobileSystemIds=array_map("getMobileSystemId",$systemList))
{
echo "Arrayfehler ArrayMap";
}
In this case, using a return in your function would be much cleaner.
Nothing to do with your problem, but is your $systemId var trusted ? (To prevent SQL injection).
Update:
if(!$mobileSystemIds=array_map("getMobileSystemId",$systemList))
{
echo "Arrayfehler ArrayMap";
}
ought to read (just checked; it works for me):
$mobileSystemIds = array_map('getMobileSystemId', $systemsList);
if (empty($mobileSystemIds))
{
if (empty($systemsList) || !(is_array($systemsList)))
echo "OK: no mobile IDs, but no systems either";
else
echo "THIS now is strange :-(";
}
else
{
echo "Alles OK";
var_dump($mobileSystemIds);
}
I tried this by returning a dummy value based on input; if it does not work for you, there must be something strange in the database.
(Update: the text below refers to your original code, which did not use array mapping)
Your code ought to be working as it is. You put several $mobileSystemId 's into a single $mobileSystemId.
It works: I tested with a simpler code, removing the DB calls but leaving your code, and spelling, untouched.
So, the error must be elsewhere. I would guess that this code is included into something else, and:
the $mobileSystemIds = array(); declaration gets executed more than once, thereby losing all its data;
the $mobileSystemIds = array(); declaration is itself included in a more local scope and you read it from outside, reading an empty value or a totally different value.
Try replacing the first part of your code with:
GLOBAL $mobileSystemsIds;
if (defined($mobileSystemsIds))
trigger_error("mobileSystemsId defined more than once", E_USER_ERROR);
else
$mobileSystemsIds = array();
and also, in the function body:
if (!defined($mobileSystemsId))
trigger_error("mobileSystemsId should have been defined", E_USER_ERROR);
Why is this warning occuring when I am using IE?
Warning: Invalid argument supplied for foreach()
It works in all other browsers..
The function for the loop:
function wdsearch(PDO $dbh){
if(!isset($_POST['wdsubmit'])) {
} else {
$term = $_POST['wdsearchvalue'];
$stmt = $dbh->prepare("
SELECT *
FROM posts
WHERE category = :designer
AND (full_text LIKE CONCAT('%', :term, '%')
OR heading LIKE CONCAT('%', :term, '%'))
ORDER BY post_date DESC
");
$designer = 'Designer';
$stmt->bindParam(":designer", $designer);
$stmt->bindParam(":term", $term);
$stmt->execute();
return $stmt->fetchAll();
}
}
wdsearch($dbh);
$wdsearch = wdsearch($dbh);
And the loop goes here..
<?php foreach($wdsearch as $wds) : ?>
<!-- HTML here -->
<?php endforeach; ?>
Any possible IE related warnings? Like I said all other browsers can handle it..
The problem is in your wdsearch function; if $_POST['wdsubmit'] is not set, it returns nothing and if anything goes wrong in any of the db operations it will not return an array either.
You need to add some error handling to your db operations and in case of failure or a non-post, return an empty array and / or check if $wdsearch is an array before you use foreach.
The specified error is occurring in your PHP code, not in any specific browser.
The issue is, the datatype of $wdsearch, does not implement an iterator. This could be caused by $wdsearch being null, a string/number/etc. - or simply an unsupporting object.
Try verifying if the data is an array before going into the foreach loop with is_array($wdsearch), or verify the variable is not null with isset($wdsearch). If you believe there is valid data there, check to see if the object in $wdsearch actually supports iterators and, if not, update it to implement!
Example:
<?php
if (isset($wdsearch)):
foreach($wdsearch as $wds) :
?>
<!-- HTML here -->
<?php
endforeach;
endif;
?>
If for the same parameters it works in Chrome and FF, but not IE.
What are exactly are you submitting in IE. Is there some silly thing like a button with picture submit? That would pass your terms as the location of the click, not actual value for example.
Debug what your incoming parameters in $POST are before outputting to foreach. Also how do you handle null result sets?
this is the code in my module file. if i only want to print the second or the third value or another value., how should i do?
function alterlink_address(){ //page callback function
$sql = db_query("SELECT field_link_url FROM {content_type_address}");
while ($q = db_fetch_object($sql)){
return $q->field_link_url.'<br>';
}
}
I'm no drupal expert and there will surely be a more economic way, but this will still work:
function alterlink_address(){ //page callback function
$sql = db_query("SELECT field_link_url FROM {content_type_address}");
while ($q = db_fetch_object($sql)){
$results[] = $q->field_link_url.'<br>';
}
return $results[0]."<br />";
}
Where the 0 in square brackets is the number (starting from 0) of the result you want to return.
A couple of notes:
a correct indentation can save lives;
getting a plethora of results from the database and displaying just a few of them is a nice method to awaken Cthulhu. I suggest you take a look at the drupal docs to get directly just the results you need.