Here is the code in my class file. I have separate class file and a product edit parts with fields. When I access them in new page I get nothing.
function editProd($editid){
include('config.php');
$query= sprintf("select * From product_data where id=".$editid."");
$result=mysqli_query($con,$query);
$data=array();
while($row=mysqli_fetch_array($result) ){
$data[]=$row;
}
return $data;
}
This is how I access it in new page
include('prductclass.php');
$addnewprod=new Productdata();
$addnewprod->editProd($_REQUEST['edit_id']);
but I get nothing in result so how do I fill my form in edit from
var_dump($addnewprod->editProd($_REQUEST['edit_id'])); should get you some output (if the sql query returned results).
If you need it in a variable:
$var = $addnewprod->editProd($_REQUEST['edit_id']);
Or:
$data = $addnewprod->editProd($_REQUEST['edit_id']);
foreach ($data as $row ) {
echo 'Column 1: ' . $row['some_column'name'] . '<br />';
echo 'Column 2: ' . $row['some_other_column'name'] . '<br /><br />';
}
You return $data, so you will have to do something with that data.
Your current code requests the data by using the code $addnewprod->editProd($_REQUEST['edit_id']); but does nothing with the data it receives.
you should get used to a better, consistent style
$myVar = 'foobar';
instead of
$myVar ='foobar';
$myVar= 'foobar';
$myVar='foobar';
Related
I've written some code in php to scrape some preferable links out of the main page of wikipedia. When I execute my script, the links are coming through accordingly.
However, at this point I've defined two functions within my script in order to learn how to pass links from one function to another. Now, my goal is to print the links in the latter function but it only prints the first link and nothing else.
If I use only this function fetch_wiki_links(), I can get several links but when i try to print the same within get_links_in_ano_func() then it prints the first link only.
How can I get them all even when I use the second function?
This is what I've written so far:
include("simple_html_dom.php");
$prefix = "https://en.wikipedia.org";
function fetch_wiki_links($prefix)
{
$weblink = "https://en.wikipedia.org/wiki/Main_Page";
$htmldoc = file_get_html($weblink);
foreach ($htmldoc->find("a[href^='/wiki/']") as $a) {
$links = $a->href . '<br>';
$absolute_links = $prefix . $links;
return $absolute_links;
}
}
function get_links_in_ano_func($absolute_links)
{
echo $absolute_links;
}
$items = fetch_wiki_links($prefix);
get_links_in_ano_func($items);
Your function returned the value at the very first iteration. You will need something like this:
function fetch_wiki_links($prefix)
{
$weblink = "https://en.wikipedia.org/wiki/Main_Page";
$htmldoc = file_get_html($weblink);
$absolute_links = array();
foreach ($htmldoc->find("a[href^='/wiki/']") as $a) {
$links = $a->href . '<br>';
$absolute_links []= $prefix . $links;
}
return implode("\n", $absolute_links);
}
I have the following function:
function create_table($titles, $id) {
$buffer = '<div id="' . $id . '">';
foreach ($titles as $k=>$v) {
$buffer .= '<input type="checkbox" name="' . $k . '" id="' . $k . '">';
$buffer .= '<label for="' . $k . '">' . $v . '</label>';
}
$buffer .= '</div>';
echo $buffer;
}
This takes the values and keys from an associative array and sets up a group of checkboxes.These are for a signup page, but I also want to use the same page for the user to edit their profile. Therefore I need a way of loading the users preferences from the database into the checkboxes 'value' property. I've thought of passing the values into the function by changing the parameters to:
function create_table($titles, $values, $id)
and then calling the function with the following parameters:
$id = "reasons";
$titles = $reasons_array;
$values = $stmt->fetch(PDO::FETCH_ASSOC);
However, if the user is trying to sign up rather than edit then the PDO Fetch will be trying to fetch from an empty database row.
If I have two different functions, one for editing one for signing up then the file will be full of if(edit==true) statements, but I cant think of another way of getting the values into the function IF the user is trying to edit! Help!
simple example
function viewTable($aData, $bEdit = false)
{
echo $aData['some'];
// ...
if ($bEdit) { // or you can use isAuth() function without arg-var
echo $aData['auth']; // hidden data only for edit page
}
}
function getData()
{
$aReturn['some'] = 123; // all vars you need;
// ...
if (isAuth()) {
$aReturn['auth'] = 321; // only for authenticated (for edit)
}
return $aReturn;
}
function isAuth()
{
return true; // check authentication
}
viewTable(getData(), isAuth());
I am trying to use base64_encode to display the ids differently. I am also trying to add a large number to the id so the string will look longer.
The problem is I encrypt a number to make it a string. Then when I decrypt the same string I expect the same value but in my case it is returning different values.
Why I am not seeing the same values?
This is my code:
define('IDS_SALT', 852045641596357);
function simple_encode($id){
$data = '';
$id += IDS_SALT;
$data = base64_encode($id);
$data = str_replace(array('+','/','='),array('-','_','.'),$data);
return $data;
}
function simple_decode($code){
$data = '';
$code = str_replace(array('-','_','.'),array('+','/','='),$code);
$id = base64_decode($code);
$id -= IDS_SALT;
return $id;
}
//this is returning "OC41MjA0NTY0MTU5NjM2RSsxNA.."
echo 'Encrypted: ';
echo simple_encode(7);
echo '<br />';
//this is returning 3 a NOT 7. it should return 7
echo 'Encrypted: ';
echo simple_decode( simple_encode(7) );
echo '<br />';
I just ran the same code (exactly as above) on my test server here - I received different results:
Encrypted: ODUyMDQ1NjQxNTk2MzY0
Encrypted: 7
Which looks to be correct?
Is there any other code in your test script that could be interfering?
Steve
I'm really stuck trying to resolve what should be quite simple.
I Have this
<?php
$json = json_decode('{
"33540116":
{"person":
{"name":"John", "age":"36"}},
"33541502":
{"person":
{"name":"Jack", "age":"23"}}
}
');
$id = array('33540116', '33541502');
foreach($id as $id) {
echo $json->$id->person->{'name'}. '<br />';
echo $json->$id->person->{'age'}. '<br />';
}
?>
So the code is decoding a json string then using foreach to echo each result.
This json file is rather large and I'm only interested in certain records that match the id's stored in a mysql table.
To do this I have replaced the id array string above with mysql select statement.
<?php
$json = json_decode('{
"33540116":
{"person":
{"name":"John", "age":"36"}},
"33541502":
{"person":
{"name":"Jack", "age":"23"}}
}
');
$result = mysql_query("SELECT id FROM people");
$row = mysql_fetch_array($result);
$id = array($row['id']);
foreach($id as $id) {
echo $json->$id->person->{'name'}. '<br />';
echo $json->$id->person->{'age'}. '<br />';
}
?>
Although this works, it only gives me 1 result.
What I really need is to loop through the results.
Unfortunately I don't know how to construct a while loop together with foreach.
I will greatly appreciate your assistance.
UPDATE (extra question)
Thanks everyone. You have helped me solve the problem.
However, I have another question that relates to this matter.
I mentioned above that I merely wanted to echo the results.
But this isn't exactly true.
What I really want to do is update the same mysql table with the results retreived from the json file.
I have a table called people with fields id, name and age.
How can I update this table with these results?
Thanks again.
mysql_fetch_array only fetches one row at a time. You can use a while loop to continue fetching rows. The mysql_fetch_array function returns false once the whole result set has been fetched, so that will cause the while loop to terminate as desired.
Also, I removed the foreach loop on $ids. Since there will only be one element in the array it's unnecessary to put the code in a loop.
<?php
$json = json_decode('{
"33540116":
{"person":
{"name":"John", "age":"36"}},
"33541502":
{"person":
{"name":"Jack", "age":"23"}}
}
');
$result = mysql_query("SELECT id FROM people");
while ( ( $row = mysql_fetch_array($result) ) ) {
$id = $row['id'];
echo $json->$id->person->{'name'}. '<br />';
echo $json->$id->person->{'age'}. '<br />';
}
?>
Try this:
while($row = mysql_fetch_array($result)) {
$id = array($row['id']);
foreach($id as $id) {
echo $json->$id->person->{'name'}. '<br />';
echo $json->$id->person->{'age'}. '<br />';
}
}
You are using this :
$row = mysql_fetch_array($result);
This will only fetch one row from the database.
If you want to fetch more than one row, you have to call mysql_fetch_array() in a loop :
while ($row = mysql_fetch_array($result)) {
// Work with the current row
}
In your case, you'd have something like this :
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
echo $json->$id->person->{'name'}. '<br />';
echo $json->$id->person->{'age'}. '<br />';
}
You do not need foreach loop the while is just enough
$result = mysql_query("SELECT id FROM people");
$id = array($row['id']);
while($row = mysql_fetch_array($result))
{
echo $json->$row['id']->person->{'name'}. '<br />';
echo $json->$row['id']->person->{'age'}. '<br />';
}
You have a couple of issues. Firstly, you shouldn't be using foreach($id as $id) since you are using the same variable for both. Instead it should be foreach($ids as $id). Secondly, you can get a list of the ids and echo out the correct json values as follows
<?php
$json = json_decode('{
"33540116":
{"person":
{"name":"John", "age":"36"}},
"33541502":
{"person":
{"name":"Jack", "age":"23"}}
}
');
$result = mysql_query("SELECT id FROM people");
$ids = array();
while($row = mysql_fetch_array($result)) {
$ids[] = $row['id'];
}
foreach($ids as $id) {
echo $json->$id->person->{'name'}. '<br />';
echo $json->$id->person->{'age'}. '<br />';
}
?>
Hopefully that will solve it
I am having problem with the yahoo search API, sometimes it works and sometimes don't why I am getting problem with that
I am using this URL
http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start
The code is given below:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<? $search = $_GET["search"];
$replace = " "; $with = "+";
$search = str_replace($replace, $with, $search);
if ($rs =
$rss->get("http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start")
)
{ }
// Go through the list powered by the search engine listed and get
// the data from each <item>
$colorCount="0";
foreach($rs['items'] as $item) { // Get the title of result
$title = $item['title']; // Get the description of the result
$description = $item['description']; // Get the link eg amazon.com
$urllink = $item['guid'];
if($colorCount%2==0) {
$color = ROW1_COLOR;
} else {
$color = ROW2_COLOR;
}
include "resulttemplate.php"; $colorCount++;
echo "\n";
}
?>
Sometimes it gives results and sometimes don't. I get this error usually
Warning: Invalid argument supplied for foreach() in /home4/thesisth/public_html/pdfsearchmachine/classes/rss.php on line 14
Can anyone help..
The error Warning: Invalid argument supplied for foreach() in /home4/thesisth/public_html/pdfsearchmachine/classes/rss.php on line 14 means the foreach construct did not receive an iterable (usually an array). Which in your case would mean the $rs['items'] is empty... maybe the search returned no results?
I would recommended adding some checks to the results of $rss->get("...") first, and also having an action for when the request fails or returns no results:
<?php
$search = isset($_GET["search"]) ? $_GET["search"] : "default search term";
$start = "something here"; // This was left out of your original code
$colorCount = "0";
$replace = " ";
$with = "+";
$search = str_replace($replace, $with, $search);
$rs = $rss->get("http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start");
if (isset($rs) && isset($rs['items'])) {
foreach ($rs['items'] as $item) {
$title = $item['title']; // Get the title of the result
$description = $item['description']; // Get the description of the result
$urllink = $item['guid']; // Get the link eg amazon.com
$color = ($colorCount % 2) ? ROW2_COLOR : ROW1_COLOR;
include "resulttemplate.php";
echo "\n";
$colorCount++;
}
}
else {
echo "Could not find any results for your search '$search'";
}
Other changes:
$start was not declared before your $rss->get("...") call
compounded the $color if/else clause into a ternary operation with fewer comparisons
I wasn't sure what the purpose of the if ($rs = $rss->get("...")) { } was, so I removed it.
I would also recommend using require instead of include as it will cause a fatal error if resulttemplate.php doesn't exist, which in my opinion is a better way to detect bugs than PHP Warnings which will continue execution. However I don't know you whole situation so it might not be of great use.
Hope that helps!
Cheers