Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm new to this, and I've looked on many different posts here, as well as on a lot of other resources found on google, but I'm still struggling with arrays.
in my html, I'm basically trying to display a sum total of a column of dollar amounts in my database.
Here's my HTML/PHP:
<div class="col-sm-2">
<h1><?php get_sum_income(); ?></h1>
</div>
and here's my external PHP file with the function it calls:
function get_sum_income() {
require(ROOT_PATH . "inc/database.php");
try {
$results = $db->prepare("SELECT SUM(cat_amount) FROM categories WHERE is_income = '1';");
$results->execute();
} catch (Exception $e) {
echo ("ERROR: Data could not be retrieved from the database." . $e);
exit;
}
$sum_income = $results->fetch(PDO::FETCH_ASSOC);
print $sum_income;
};
I keep getting an "array to string conversion" error, and I know it has to do with my understanding of arrays (or lack thereof). I've looked around, and I just need help with my specific situation.
Also, is how I'm accessing the function the best way to do this?
You can't print an array. print is for scalars. PHP is trying to convert your array to a string so that it can print it, but even its own attempt at this fails, because it's simply not supported.
You can print_r it, though!
Update Sounds like you want to retrieve, on its own, the single element of the array that is your database query result. It's in an array because PHP doesn't know ahead of time that your resultset will only have a single column in it.
In your case, you'd write:
print $sum_income['SUM(cat_amount)'];
This is pretty unwieldy, though. You can shorten it by giving the column an alias in your MySQL query:
SELECT SUM(`cat_amount`) AS `mySum` FROM `categories` WHERE `is_income` = '1';
Then:
$row = $results->fetch(PDO::FETCH_ASSOC);
print $row['mySum'];
(I've improved the variable name, too!)
You can't print or echo arrays. You can, however, print a specific element of it:
echo $myArray['foo'];
Or, you can var_dump or print_r it.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an HTML table that I would like to parse in PHP to store into a MySQL Database. The HTML looks like this:
<tr><td>DATE</td><td>LOCATION</td><td>NAME</td></tr>
I would like to create a PHP function that returns in an array, the fields in capital letters. Does anyone know any php libraries that can do this, or should I be using a different language, as this may be complex. I don't know exactly how to do this with many tables on the page, but I am trying to parse the VEX events on RobotEvents. The table that I want to parse starts at line 465.
Take a look at the PHP HTML DOM Parser library.
To use, you can do something similar to this (not my example):
require('simple_html_dom.php');
$table = array();
$html = file_get_html('http://flow935.com/playlist/flowhis.HTM');
foreach($html->find('tr') as $row) {
$time = $row->find('td',0)->plaintext;
$artist = $row->find('td',1)->plaintext;
$title = $row->find('td',2)->plaintext;
$table[$artist][$title] = true;
}
echo '<pre>';
print_r($table);
echo '</pre>';
There's some tutorials, SO questions and interesting reads about the library. It seems to be pretty popular.
http://davidwalsh.name/php-notifications
http://net.tutsplus.com/tutorials/php/html-parsing-and-screen-scraping-with-the-simple-html-dom-library/
Looping through a table with Simple HTML DOM
how to print cells of a table with simple html dom
UPDATE FOR FINDING SPECIFIC TABLE IN HTML USING ABOVE LIBRARY
To find a particular table amongst many:
1. By class:
On line 465 of your scraped HTML, the table starts with a class catalog-listing, so:
foreach ($html->find('table[#class="catalog-listing"]')->find('tr') as $row) {
// extract TD data
}
2. By instance (find 2nd table in HTML)
foreach ($html->find('table', 2)->find('tr') as $row) {
// extract TD data
}
As you're prepared to look beyond PHP, Nokogiri (Ruby) and Beautiful Soup (Python) are well-established libraries that parse HTML very well.
That doesn't imply that there are no suitable PHP libraries.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This one may be a bit of a doozy.
I have 2 sets of files in separate folders:
SET 1:
celeb1.png
celeb2.png
celeb3.png
celeb4.png
celeb5.png
etc
The user selects an image (radio) and I then set this as a get variable on the url:
Now, I want to match another file.
In another folder, I have a set like this:
SET 2:
celeb1-24_59_250_300.png
celeb2-35_67_200_250.png
celeb3-54_87_300_400.png
celeb4-88_98_250_350.png
celeb5-87_43_300_400.png
etc
I want to use the GET variable (i.e.) celeb1.png, and then select the full filename of the corresponding file, based on the first 5 characters.
In other words if $_GET['celebimg'] is 'celeb1.png', I want to set a second variable (let's say $overlay) to string 'celeb1-24_59_250_300.png'
Any idea on how to achieve that?
I could obviously do a switch, but that means I would have to update the code every time a new celeb image is uploaded.
Is there any dynamic way to achieve this?
Thanks
JG
You can try something like this. Since we haven't seen what you have tried so far. This is just an illustration. However the code has been tested.
<?php
$file='celeb3.png'; // Here is where you will get the param $_GET['set1']
$set2=array(
'celeb1-24_59_250_300.png',
'celeb2-35_67_200_250.png',
'celeb3-54_87_300_400.png',
'celeb4-88_98_250_350.png',
'celeb5-87_43_300_400.png'
);
$set1 = explode('.',$file);
//echo $set1[0];//celeb3
foreach($set2 as $val)
{
if($set1[0]==substr($val,0,strlen($set1[0])))
{
echo $val;//celeb3-54_87_300_400.png
break;
}
}
OUTPUT : celeb3-54_87_300_400.png
Use glob to search a folder
//Get the GET variable
$cimg = $_GET['celebimg'];
//split it so we can just get the name without extention
$nameparts = explode(".",$cimg);
//glob will try to find matches to the string passed
$matches = glob("/dir/path/{$nameparts[0]}-*.png");
$matches will be an array of matched files
you could use other file functions like scandir or readdir etc but those do not filter out the files so you would have to do it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to create an CMS-system where I can add photos to my website, and for the photo-part, I want to have the ability to enter which programs I had used to create that photo.
I should be possible to add more than one program.
Right now I am saving the programs in the database like this:
In the programs-row:
photoshop illistrator indesign
Then at my website, it could be nice if the icons/logos of the used programs, could show up next to the photo.
So my question is how to do create a new div, what a new class, based on the words from the programs-row?
Fx:
photoshop illustrator indesign
Becomes to:
<div class="photoshop"></div>
<div class="illustrator"></div>
<div class="indesign"></div>
Hope you guys can help me with this problem :)
Thanks ;)
Use the explode function and a foreach loop to perform the string manipulation.
$programs = explode(" ", $data);
foreach($programs as $value) {
//Echo out the html - $value contains the program name
}
I leave it to you to figure out how to format the program name with the HTML that you need.
$fx = "photoshop illistrator indesign";
$words = explode(" ", $fx);
array_walk($words, function(&$n) {
echo '<div class="'.$n.'"></div>';
});
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Say you have:
$text1="i can wait for all night";
$text2="we can sing for all day";
I need a function that will tell me 3 words found to be the same in both statements
i.e can, for, all.
Thanx
You can use explode to get the array of words:
$text1="i can wait for all night";
$text2="we can sing for all day";
$text1arr = explode(" ", $text1);
$text2arr = explode(" ", $text2);
Then, you can use array_intersect to get the common words:
$result = array_intersect($text1arr , $text2arr);
Finally, you can use count to get the number of common words:
$num_in_common = [count][3]($result);
$words1 = str_word_count($text1, 2);
$words2 = str_word_count($text2, 2);
$common_words = $result = array_intersect($words1, $words2);
$common_count = sizeof($common_words);
As a heads up, SO is not a coding service, and people won't be happy when answers are simply asked for, with seemingly no effort on the part of the asker. However, here's a little bit to get you started.
The array_intersect function, when given two or more arrays, gives you an array of all of the values shared between the inputs.
As for finding the position of the values, the array_search function gives a key for a value in the array.
If all arrays share the same key for a value in the array_intersect output array, then you know that that value is shared by all arrays, and in the same position.
How to implement all this is up to you.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this script in PHP like this:
echo "The results is $res";
And now, I want make a new variable to save that script. What can I do?
I do this because the value of $res is always changing depending on the input entered from user.
If I've understood you correctly, you want to save the script into a variable (let's call it $script), so that when $res="foo", the result of echo $script; is "The result is foo" and when $res="bar", echo $script; returns "The result is bar".
I don't think you can do this using a normal variable, but you certainly could do it as a function:
//The function
function get_res($res){
return "The result is $res";
}
//The function call
$res = "foo";
print get_res($res);
$res = "bar";
print get_res($res);
This would output "The result is foo" the first time the function is called, and "The result is bar" the second time.
$res = 'some resource';
$echo = 'The result is ' . $res;
echo $echo;
Is that what you want? If not, please clarify / simplify your question.