I want to display information from multiple rows. Without changing the majority of my code, how can I echo the multiple rows on my webpage that have user_id_offerer = '$offerer'. I did echo the mysql query and I got the result of Resource Id #14 and I understand a while loop is necessary, but it seems like there is a problem with mysql. How do I fix this?
Clarification: When I ran the code with the while loop, I received an infinite loop of the first row. I did not receive each individual row that had user_id_offerer = 'offerer'. How do I fix this without having to do a major re-write of the code.
function ergo_data ($offerer) {
$data = array();
$offerer = (int)$offerer;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if ($func_num_args > 1) {
unset($func_get_args[0]);
$fields ='`' . implode ('`, `', $func_get_args) . '`';
$data = mysql_fetch_assoc (mysql_query("SELECT $fields FROM `ergo` WHERE `user_id_offerer` = '$offerer'"));
return $data;
}
}
if (logged_in() === true) {
$session_user_id = $_SESSION['user_id'];
$ergo_data = ergo_data($session_user_id, 'primary_key', 'user_id_offerer', 'user_id_seeker', 'ergo', 'ergo_time_submitted');
}
echo $ergo_data['ergo'] . '<br>';
echo username_from_user_id($ergo_data['user_id_offerer']) . '<br>';
echo username_from_user_id($ergo_data['user_id_seeker']) . '<br>';
echo $ergo_data['ergo_time_submitted'];`
I think you need to return the result of mysql_query from your ergo_data() function (instead of the result of mysql_fetch_assoc), then call mysql_fetch_assoc in the loop:
// inside ergo_data(), change
// $data = mysql_fetch_assoc (mysql_query("SE ...
// to:
function ergo_data ($offerer) {
...
return mysql_query("SELECT $fields FROM `ergo`....");
}
...
if (logged_in() === true) {
$session_user_id = $_SESSION['user_id'];
$query_result = ergo_data($session_user_id, 'primary_key', 'user_id_offerer', 'user_id_seeker', 'ergo', 'ergo_time_submitted');
while($ergo_data = mysql_fetch_assoc($query_result)) {
echo $ergo_data['ergo'] . '<br>';
echo username_from_user_id($ergo_data['user_id_offerer']) . '<br>';
echo username_from_user_id($ergo_data['user_id_seeker']) . '<br>';
echo $ergo_data['ergo_time_submitted'];
}
}
Related
I am trying to use return 2 different arrays using the same random variable. So I have:
function TransTest() {
$TransQ = array();
$TransA = array();
$TransQ[0] = "Q1";
$TransQ[1] = "Q2";
$TransQ[2] = "Q3";
$TransQ[3] = "Q4";
$TransA[0] = "Ans 1";
$TransA[1] = "Ans 2";
$TransA[2] = "Ans 3";
$TransA[3] = "Ans 4";
$index = rand(0, count($TransQ)-1);
return $TransQ[$index];
}
So this basically returns a random Question from the $TransQ array. What I would like to do is return the respective answer to the question.
Something similar to:
return ($TransQ[$index] && $TransA[$index]);
But this doesn't seem to work. Please help.
Just return an array:
return array($TransQ[$index], $TransA[$index]);
Then access:
$result = TransTest();
echo $result[0] . ' is ' . $result[1];
Or associative:
return array('q' => $TransQ[$index], 'a' => $TransA[$index]);
Then:
$result = TransTest();
echo $result['q'] . ' is ' . $result['a'];
Or in each of the above cases:
list($question, $answer) = TransTest();
echo $question . ' is ' . $answer;
Another way (probably not the best choice for your case), is to use references &:
function TransTest(&$q=null, &$a=null) {
//code
$q = $TransQ[$index];
$a = $TransA[$index];
}
Then:
TransTest($question, $answer);
echo $question . ' is ' . $answer;
So I have an array and form with input where I put an ID that should show the corresponding row from the array.
So far I have made this:
<?php
$file = 'people.txt';
$people = array();
$ref = fopen($file, 'r');
while ($row = fgets($ref, 1024)) {
$row = explode("|", $row);
$id = $row[0];
$name = $row[1];
$status = $row[2];
$people[$id] = array('name' => $name, 'status' => $status);
}
fclose($ref);
foreach($people as $id => $person) {
if($_GET[person]==$id && $person[status]==1) {
echo $person[name] . "(" . $id . "): ready";
}
if($_GET[person]==$id && $person[status]==0) {
echo $person[name] . "(" . $id . "): not ready";
}
if($_GET[person]!=$id) {
echo "Person with this ID was not found";
}
}
?>
This shows the correct info of searched ID, but it also shows "person was not found" to every other person in the array. How can I get it to show only the person that matches the input?
There's no need AT ALL for the loop. If you already have the ID of the person, then there's no need to scan the array for that id, just use it directly:
if (isset($people[$id])) {
... do stuff with person # $id
} else {
... person does not exist
}
Unless you need the array of all the people for some other purpose after this code, you can get only the person indicated by $_GET['person'] from the file. Just add a check inside the while loop.
$person = null; // initialize person
$ref = fopen($file, 'r');
while ($row = fgets($ref, 1024)) {
$row = explode("|", $row);
if ($row[0] == $_GET['person']) { // only get rows that match the searched id
$person = array('name' => $row[1], 'status' => $row[2]);
}
}
fclose($ref);
This will reduce the memory usage of your script by avoiding creating an array of the entire file. The benefit of this obviously depends on the size of the file.
(Even if you do need the entire array, you can still set $person in an if block in your while loop at the same time you are filling the $people array.)
Then after that loop, $person will be just one thing: either an array with the last matched row in your file, or null. That means you won't have to find it in an array; you can output the status information like this:
if ($person) {
echo "$person[name] ($person_id): ";
echo $person['status'] ? 'ready' : 'not ready';
} else {
echo "Person with this ID was not found";
}
Since you are only searching for one "person ID", you do not need the second foreach loop in your code. I recommend refactoring a bit to include more error checking and also reading about type juggling and casting in PHP.
I've modified your code to include some hints that should point you in the right direction:
<?php
$file = 'people.txt';
$people = array();
$ref = fopen($file, 'r');
while ($row = fgets($ref, 1024)) {
$row = explode('|', $row);
if (!isset($row[0]) || !isset($row[1]) || !isset($row[2])) {
continue;
}
$id = (int) $row[0];
$name = (string) $row[1];
$status = (int) $row[2];
$people[$id] = array('name' => $name, 'status' => $status);
}
fclose($ref);
$id = array_key_exists('person', $_GET) ? $_GET['person'] : null;
if (array_key_exists($id, $people)) {
$person = $people[$id];
if ($person['status'] === 1) {
echo $person['name'] . ' (' . $id . '): ready';
} else {
echo $person['name'] . ' (' . $id . '): not ready';
}
} else {
echo 'Person with ID ' . htmlspecialchars($id) . ' not found';
}
If you plan on people.txt becoming large in size, consider storing the data in a database such as MySQL. This will eliminate having to read the contents of the file into memory on every request.
Use a boolean variable to detect if the person you're searching for is found or not, The code could be like this:
$found = false;
foreach($people as $id => $person) {
if($_GET[person]==$id) {
$found = true;
if($person[status]==1)
echo $person[name] . "(" . $id . "): ready";
else if($person[status]==0)
echo $person[name] . "(" . $id . "): not ready";
//You can (break;) here if you're sure that the $id is identical
}
}
if($found == false)
echo "Person with this ID was not found";
I can't understand and I seek for help=(
Here is my code:
$add_article = $this->M_articles->Add($_POST['title'], $_POST['content']);
echo "sd;flksdf;lksdfl;";
$add_article2 = true;
if ($add_article)
{
echo 'Article Added!';
header("Location:index.php");
die();
}
else
die('Error adding article');
this is function "Add" from M_Articles:
public function Add($title, $content)
{
/*
$title = trim($title);
$content = trim($content);
if ($title == '')
return false;
//запрос
$object = array();
$object['title'] = $title;
$object['content'] = $content;
$this->msql->Insert('articles', $object);
*/
return true;
}
The thing is...even if I comment everything from function "Add" and leave only "return true"... it wouldn't redirect me to index.php. Moreover, it doesn't even echo anything (even those "sd;fkfdsf.." string). The script just dies for some reason. I can't get where is the problem, can some1 explain to newbie what's the problem and how it should be fixed? If you need additional info, i'll provide it.
update: Maybe it's important...but if I delete those comment "/* */" things, it'd correctly add article to a DataBase. But then the script dies=/
update:
ok, now it says: "Notice: Undefined variable: result in Z:\home\myblog\www\c\M_MSQL.php on line 86"
here's my code for M_MSQL on line 86:
public function Insert($table, $object)
{
$columns = array();
$values = array();
foreach ($object as $key => $value)
{
$key = mysql_real_escape_string($key . '');
$columns[] = $key;
if ($value === null)
{
$values[] = "'$value'";
}
else
{
$value = mysql_real_escape_string($value . '');
$values[] = "'$value'";
}
}
$columns_s = implode(',', $columns);
$values_s = implode(',', $values);
$query = "INSERT INTO $table ($columns_s) VALUES ($values_s)";
$result = mysql_query($query);
if (!$result)
die(mysql_error());
return mysql_insert_id();
}
Reason is you are outputing things, so you have a notice : "Headers already sent".
If you remove the "echo" stuff, you'll be alright :
if ($add_article)
{
header('Location: /index.php?article_added=1');
}
I am querying my database for results (which all display correctly) and then while running a foreach statement I am attempting to look in another table for a matching ID to gather an override price which will take the place of the 'storeprice' listed in the first result.
Here is my model:
public function fetch_products($limit, $start, $manuid) {
$this->db->order_by('productname', 'ASC');
$this->db->limit($limit, $start);
$query = $this->db->get_where('products', array('manuid' => $manuid, 'active' => 1));
if($query->num_rows() > 0){
foreach($query->result() as $row){
$data[] = $row;
$pid = $row->id;
// Check for Price Override
$squery = $this->db->get_where('price_override', array('id' => $pid));
if($squery->num_rows() > 0){
$result = $squery->row();
$override = $result->storeprice;
$data['override'] = $override;
} else {
$override = 0;
$data['override'] = $override;
}
}
return $data;
}
return false;
}
The $data['override'] is what is causing me an error. The following error to be precise:
Message: Undefined property: stdClass::$override
In the controller I am using the following:
$data['results'] = $this->store_products_model->fetch_products($config["per_page"], $page, $manuid);
And finally in the view I am calling the results inside of a table to display. All will display except for the override:
foreach ($results as $product){
echo '<tr>';
echo '<td>' . $product->productname . '</td>';
if($product->override != 0){
echo '<td>$' . $product->override . '</td>';
} else {
echo '<td>$' . $product->storeprice . '</td>';
}
echo '<td>$' . $product->saleprice . '</td>';
echo '<td>' . $product->storepoints . '</td>';
echo '<td>Edit';
echo '</tr>';
}
Anyone see what I could be doing wrong here to give me the following error?
Message: Undefined property: stdClass::$override
The row is an object, not an array. Try:
$data->override = $override
edit:
Well, actually, $data is an array, but your inserting an object into it, so it would be
$last_index = sizeof($data) - 1;
$data[$last_index]->override = $override;
I have a list of images within a file. The file name along with details about the image are stored in a mysql table. I have created a while loop that randomly generates several images on my page. There is a problem however. My webpage is not producing any images. I have narrowed it down to the function I created clothing_data. I'm just stumped at what I need to do. Here is the code below.
<?php
function clothing_data ($id) {
$data = array();
$id = (int)$id;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if ($func_num_args > 1) {
unset($func_get_args[0]);
$fields ='`' . implode ('`, `', $func_get_args) . '`';
$data = mysql_fetch_assoc (mysql_query("SELECT $fields FROM `dress` WHERE `primary_id` = $id"));
return $data;
}
}
$num_dresses = dress_count ();
$i = 0;
while ($i < 5)
{
$rand_id = rand(1, $num_dresses);
$dress_feed_data = clothing_data($rand_id, 'file_name', 'user_defined_name',
'user_defined_place' , 'user_who_uploaded');
echo $dress_feed_data['file_name'];
if (file_exists('fashion_images/' . $dress_feed_data['file_name']))
{
?>
<br>
<img src="fashion_images/<?php echo $dress_feed_data['file_name'];?>" width="50" height="50" />
<?php
}
$i++;
}
?>
In your <img> tag $dress_feed_data[file_name] should be $dress_feed_data['file_name'].
Most likely $field is either empty or contains a wrong column name or other error. What do you get when you var_dump()?
Since you already defining the columns in $dress_feed_data = clothing_data($rand_id, 'file_name', 'user_defined_name',
'user_defined_place' , 'user_who_uploaded');
you could simply just change your function to something like-
function clothing_data ($id, $field1, $field2, $field3, $field4) {
$data = array();
$id = (int)$id;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
$fields ='`' . $field1 . '`, `' . $field2 . '`, `' . $field3 . '`, `' . $field4 . '`';
$data = mysql_fetch_assoc (mysql_query("SELECT $fields FROM `dress` WHERE `primary_id` = $id"));
return $data;
}
I fixed the problem. There were multiple issues involved. For one my primary id was not linked up to the randomly generated ids that were inside my database. The other reason it was not working was because the fields relating to the name of my filename were not the same ones found in my images folder. This is because when the name of the file is inserted into my database a space is added to the name making it impossible to link the name on my database to the name in my folder. Thanks for all the help guys.