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.
Related
I created this in the controller to export the products in json format, but I only get them in 1 language, I need them in two languages What could I do in this case I need help!
<?php
class ControllerApiProduct extends Controller
{
public function index()
{
$this->load->language('api/cart');
$this->load->model('catalog/product');
$this->load->model('tool/image');
$json = array();
$json['products'] = array();
$filter_data = array();
$results = $this->model_catalog_product->getProducts($filter_data);
foreach ($results as $result) {
$data['products'][] = array(
'name' => $result['name'],
);
}
$json['products'] = $data['products'];
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
}
METHOD 1 Set the language id after you're current foreach and fetch the data again. $this->config->set('config_language_id', LANGUAGE_ID_HERE). Ensure you reset it afterwards.
METHOD 2
It would be more efficient to update the model method so you can pass an array of language ids getProducts() in the file
catalog/model/catalog/product.php
Example
public function getProducts(array $data = [], array $language_ids = []): array {
and change the query to something like this where it will fetch records matching the array of given language ids
From
WHERE pd.`language_id` = '" . (int)$this->config->get('config_language_id') . "' AND p.`status` = '1' AND p.`date_available` <= NOW() AND p2s.`store_id` = '" . (int)$this->config->get('config_store_id') . "'";
To
WHERE pd.`language_id` IN (" . !empty($language_ids) ? implode(',', $language_ids) : (int) $this->config->get('config_language_id') . ") AND p.`status` = '1' AND p.`date_available` <= NOW() AND p2s.`store_id` = '" . (int)$this->config->get('config_store_id') . "'";
One day I'm on this problem and I cant find the solution.
My goal is to return a list of links from an ID (the father). So I want all his childs.
But in the view, I only have on result (the 1st one...).
I have in my controller :
$data['list_link'] = $this->menuManager->list_link();
In my Model :
function list_link($fatherid=0){
$r = array();
$sSQL = 'SELECT * FROM categories WHERE fatherid = ' . $fatherid . ' AND siteid = ' . MY_SITEID_DEFAULT . ' ORDER BY name';
$query = $this->db->query($sSQL);
// stock results in array
$r[] = $query->result_array();
foreach ($query->result() as $row) {
// let's find the childs
$this->list_link($row->id,$loop);
}
return $r;
}
If I "for each" the $r here, all looks good.
So, $data['list_link'] shoud now have all the rows.
In my view :
foreach ($list_link as $link){
foreach ($link as $row){
echo $row['name'];
}
}
But, I only have the first links (first childs), not the other one. Any help would be greatly appreciated as I'm on that problem for days...
You're not storing any values in the recursive calls (though I'm still not sure you'd get what you expect). You'd need to populate $r with each function call:
$r[] = $this->list_link($row->id,$loop);
However, either I missed something or you're overcomplicating things, but I think you could simply return the result array and use it:
function list_link($fatherid=0,$loop=0){
$sSQL = 'SELECT * FROM categories WHERE fatherid = ' . $fatherid . ' AND siteid = ' . MY_SITEID_DEFAULT . ' ORDER BY name';
$query = $this->db->query($sSQL);
return $query->result_array();
}
UPDATE
Your latest version still doesn't collect the data from recursive calls, here is the full function, see if it works:
function list_link($fatherid=0){
$r = array();
$sSQL = 'SELECT * FROM categories WHERE fatherid = ' . $fatherid . ' AND siteid = ' . MY_SITEID_DEFAULT . ' ORDER BY name';
$query = $this->db->query($sSQL);
$result = $query->result_array();
// stock results in array
$r[$fatherid] = $result;
foreach ($result as $row) {
// let's find the children
$r[$fatherid][$row['id']] = $this->list_link($row['id']);
}
return $r;
}
Note that I've added $r[$fatherid][$row['id']] so the end result should be an array with a branching structure. If you don't want that, just do $r[] instead.
I'm trying to capture a user's checkbox selection from a dynamically generated list of options but I'm getting "Array" instead of the names selected.
In my last test, I checked 2 of the three boxes and got 3 instances of "Array" back.
This is my query that finds the names:
$query = "SELECT id, first_name, last_name
FROM users WHERE location_id = " . $location_id;
$result = mysqli_query($connection, $query);
if (!$result)
{
die("Database query failed: " . mysqli_error($connection));
}
while ($row = mysqli_fetch_array($result))
{
$name = $row['first_name'] . " " . $row['last_name'];
echo '<label><input type="checkbox" name="emp_name[]" id="emp_name[]" value="' . $name . '"> ' . $name . '</label>';
}
I then post the array of names like this:
$attendee = $_POST['emp_name'];
And try to generate the list of names for my email like this:
$values = ('emp_name');
foreach ($values as $value) {
$$value = (array)$_POST[$value];
}
for($i = 0; $i < count($attendee); $i++){
$attendees = array();
foreach ($values as $value) {
$attendees[] = ${$value}[$i];
}
Can someone help me see what's wrong?
I changed my code and included a part of the html that I'm trying to render:
foreach ($attendee as $name) {
$attendees[] = $name;
$htmlBody .= "<tr><td>{$attendees}</td></tr>";
}
But it still returns Array.
Not sure why you do this:
$attendee = (array)$_POST['emp_name'];
Try to replace it with:
$attendee = $_POST['emp_name'];
From your question it's not too clear, but I think you're creating an array within an array,and that that's the issue.
If the above doesn't help, try to var_dump() the variable that gives you "Array" response, and you'll see what's within it.
EDIT: In addition to the change above, try to make this change too:
Replace all this code:
$values = ('emp_name');
foreach ($values as $value) {
$$value = (array)$_POST[$value];
}
for($i = 0; $i < count($attendee); $i++){
$attendees = array();
foreach ($values as $value) {
$attendees[] = ${$value}[$i];
}
with:
foreach ($_POST['emp_name'] as $name) {
$attendees[] = $name;
}
EDIT #2:
According to your code:
foreach ($attendee as $name) {
$attendees[] = $name;
$htmlBody .= "<tr><td>{$attendees}</td></tr>";
}
It's totally normal that you're getting Array instead of a name. See, by writing:
$attendees[] = $name;
you're implying to php that it's an array. On the other hand the foreach loop does the opposite thing, it takes each of the values from $attendee array and makes it available to you as $name variable. So you're basically doing the right thing, and then reverting it back to array for no reason.
SOLUTION:
Replace {$attendees} with {$name}. And it should work.
You can write that with alot less code.
$attendees will now contain your checked checkboxes.
$attendees = array()
foreach($_POST['emp_name'] as $name){
$attendees[] = $name;
}
I currently have been able to add one row of data from a CSV into the database but I am unable to add the next row and the next row.
I am simply asking why is it that only one row is being inserted into the database and is their a fix which would make all rows start inserting into the database?
Code Below:
include('config.php');
$file = "test.csv";
$separator = ",";
$length = 0; // size of the longest line(!), 0 = no limit
$fields = array('title', 'firstName', 'secondName', 'emailAddress', 'houseNumber', 'mobileNumber', 'address1', 'address2', 'address3', 'address4', 'postcode'); // use it as a white list
$handle = fopen($file, "r");
$header = array_flip(fgetcsv($handle, $length, $separator));
$values = array();
$i = 1;
while(($csvData = fgetcsv($handle, $length, $separator)) !== false){
echo $i." - You have inserted another row of data into the database.<br>";
foreach ($fields as $field){ // put all values in an array in correct order
$values[] = $csvData[$header[$field]];
}
mysql_query("INSERT INTO csv (" . implode(',', array_keys($header)) . ") VALUES ('" . implode("','", $values) . "')");
$i++;
}
fclose($handle);
You are using $header to map the values into $fields order so your insert should have the fields in $fields order instead of $header order.
mysql_query("INSERT INTO csv (" . implode(',', $fields) . ") VALUES ('" . implode("','", $values) . "')");
also it would be a good idea to sanitize the data to make sure it is properly escaped.
$values = array_map("mysql_real_escape_string", $values);
before your mysql_query call should do it.
Also you are not re initializing $values with each iteration so when inserting row 2 you get fields for row 1 + row 2.
so after you open your while statement do
$values = array();
to reinitialize it.
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'];
}
}