How to remove "warning:Cannot use a scalar value as an array" - php

here is my code:
$i = 0;
$list4 = array();
while($row_sent = $GLOBALS['db']->sql_fetchrow($res_sent))
{
$sql_sent2 = "SELECT * FROM ".$GLOBALS['table']['sent']." WHERE `sent_id` ='".$row_sent['sent_id']."'";
//echo $sql_size2; exit;
$res_sent2 = $GLOBALS['db']->sql_query($sql_sent2);
$num = $GLOBALS['db']->sql_numrows($res_sent2);
if($num > 0)
{
while($row_sent2 = $GLOBALS['db']->sql_fetchrow($res_sent2))
{
$list4[$i]['sent_id'] = $row_sent2['sent_id']; //this line shows error
$list4[$i]['sent_name'] = $row_sent2['sent_name'];//this line shows error
$list4[$i]['sent_qty'] = $row_sent['sent_qty'];//this line shows error
}
}
else
{
$list4=0;
}
$i++;
}

try this.
declare $list4 as an array before your loop start.
$list4[] = array();
this may be useful.
Thanks.

Related

Undefined variable: error in adding field into db

I'm getting error:Undefined variable: error
in my code:
public function add(){
$this->polls_model->rules = Pf::event()->trigger("filter","polls-adding-validation-rule",$this->polls_model->rules);
$template = null;
$template = Pf::event()->trigger("filter","polls-add-template",$template);
if ($this->request->is_post()){
$data = array();
$data["polls_question"] = $this->post->{"polls_question"};
$data["polls_pubdate"] = str_to_mysqldate($this->post->{"polls_pubdate"},$this->polls_model->elements_value["polls_pubdate"],"Y-m-d H:i:s");
$data["polls_unpubdate"] = str_to_mysqldate($this->post->{"polls_unpubdate"},$this->polls_model->elements_value["polls_unpubdate"],"Y-m-d H:i:s");
if (is_array($this->post->{"polls_status"})){
$data["polls_status"] = implode(",",$this->post->{"polls_status"});
}else{
$data["polls_status"] = $this->post->{"polls_status"};
}
$port_answer = isset($this->post->{"answer"}) ? $this->post->{"answer"} : array();
$data = Pf::event()->trigger("filter","polls-post-data",$data);
$data = Pf::event()->trigger("filter","polls-adding-post-data",$data);
$var = array();
$pollq_multiple_yes = intval($this->post->{'pollq_multiple_yes'});
$data['polls_multiple'] = 0;
if ($pollq_multiple_yes == 1) {
if(intval($this->post->{'pollq_multiple'}) > count($port_answer)){
$data['polls_multiple'] = 1;
}else{
$data['polls_multiple'] = intval($this->post->{'pollq_multiple'});
}
} else {
$data['polls_multiple'] = 1;
}
//debug($data);
Pf::database()->query('START TRANSACTION');
$inserted = $this->polls_model->insert($data);
if($inserted === false){
Pf::database()->query('ROLLBACK');
}else{
$new_id = $this->polls_model->insert_id();
$insert_meta = true;
if(count($port_answer) > 0){
$custom = array();
$int = count($port_answer);
for ($i = 0; $i < $int ; $i++) {
if(!empty($port_answer[$i])){
$custom = array(
'pollsa_qid' => $new_id,
'pollsa_answers' => e($port_answer[$i]),
);
}
$insert_meta = $this->answers_model->insert($custom);
}
if($insert_meta === false){
Pf::database()->query('ROLLBACK');
}else{
Pf::database()->query('COMMIT');
}
}
Pf::database()->query('COMMIT');
}
$errors = Pf::validator()->get_readable_errors(false);
foreach ($errors as $key => $value) {
$error[$key][0] = $errors[$key][0];
}
$this->view->errors = $error; // error here!
$var['content'] = $this->view->fetch($template);
if (count($error) > 0){// and here!!!
$var['error'] = 1;
}else{
Pf::event()->trigger("action","polls-add-successfully",$this->polls_model->insert_id(),$data);
$var['error'] = 0;
$var['url'] = admin_url($this->action.'=index&ajax=&id=&token=');
}
echo json_encode($var);
}else{
$this->view->render($template);
}
}
I edited code, added function code.
This is my add function, if I want add poll with answers.
It gives me this error to my log.
I found this tutorial Undefined Variable error in View
I've googled it but didnt find anything special what helps me out.
Initiate the variable as an array.
because if the $error is empty the compiler will see it as an array.
if not it will get an error.
$error = [];
$errors = Pf::validator()->get_readable_errors(false);
foreach ($errors as $key => $value) {
// $error[$key][0] = $errors[$key][0];
// the right way is below
// i actually dont know what you want to do but this is the right way
// but providing [0] will make it some how constant.
$error[$key] = $errors[$key]
}
$this->view->errors = $error; // error showing here!
$var['content'] = $this->view->fetch($template);
if (count($error) > 0){ // and here???
$var['error'] = 1;
}else{
Pf::event()->trigger("action","polls-add-successfully",$this->polls_model->insert_id(),$data);
$var['error'] = 0;
$var['url'] = admin_url($this->action.'=index&ajax=&id=&token=');
}

Undefined Offset: 2 (lines 22, 28, 29)

The following code shows an error
"Undefined offset: 2" on lines 22, 28, and 29
$sql = "SELECT email FROM CommercialEmails WHERE dripid = 1 AND sent='a'";
if ($resultsd1 = mysqli_query($conn, $sql )) {
$affectedrows = mysqli_num_rows($resultsd1);
while ($row = mysqli_fetch_row($resultsd1)){
$results = $row[0];
global $results;
}
}
$broken = explode(' ', $results);
$hi = 0;
$hello = 0;
a:
/**Line 22 **/ if (substr($broken[$hi], -4) == "com," && $broken[$hi] == "qwert"){
$hey[$hi] = $broken[$hi];
$hello++;
}
If(substr($broken[$hi], -4) !== "com,"){ // line 28
$hey[$hi] = $broken[$hi]; //Line 29
}
$hi++;
if ($hi == $affectedrows){
if (!isset($hey)){
echo "There are no emails";
} else {
foreach( $hey as $key => $value){
echo $value;
}
echo $hey;
}
}else{
goto a;
}
I am not sure what you are trying to achieve, but to get your code just working try this way:
$sql = "SELECT email FROM CommercialEmails WHERE dripid = 1 AND sent='a'";
$results = array();
$hey = array();
if ($resultsd1 = mysqli_query($conn, $sql )) {
$affectedrows = mysqli_num_rows($resultsd1);
while ($row = mysqli_fetch_row($resultsd1)){
$results[] = $row;
If(substr($row[0], -4) !== "com,"){
$hey[] = $row[0];
}
}
}
if (count($hey)==0){
echo "There are no emails";
} else {
foreach( $hey as $value){
echo $value;
}
echo $hey;
}
As you can see I completely removed many of your variables you don't need them until you can explain your goals.
And I've removed your weird condition if:
if (substr($broken[$hi], -4) == "com," && $broken[$hi] == "qwert"){
$hey[$hi] = $broken[$hi];
$hello++;
}
Because there is no such value of $broken[$hi] that can be equal 'qwert' and to have 'com' as substring inside. So this codition is always false and we can delete it.
Try to explain your goals. Hope I can help you.
It means that the $broken array does not have 3 elements (ofset index = 2).
I can not see where that $hi var is being set to 2 but it is somewhere in code you have not shown I guess.
Instead of global $results; inside the while loop, before the while loop starts just declare $results as an empty array $results = array();
Then after the while loop ends do print_r($results); to see if it has the contents you expect.

PHP: Undefined offset in stripos

Hello! Im working with AJAX with DB and when trying to render my db in an option and select tag it gives me an "Undefined offset error".
Here is my code:
$sql = "SELECT word FROM words";
$result = mysql_query($sql);
$response = "";
$size = 0;
if($result === FALSE) {
die(mysql_error());
}
while ($row = mysql_fetch_array($result)) {
for($i = 0; $i < count($row); $i ++) {
$pos = stripos(strtolower($row[$i]), $pattern); //Here marks the error
if(!($pos === false)) {
$size ++;
$word = $row[$i];
$response .= "<option value=\"$word\">$word</option>";
}
}
}
if($size > 0) {
echo "<select id=\"list\" size=$size onclick=\"selectValue()\">$response</select>";
}
The idea of this app is you can start typing any word and it will search for words that matches with the input, displaying it first in an option HTML tag and when no more options are matched it's displayed in a select HTML tag.
It's kind of working but it displays this errors. Can someone help me? Thanks!!
Here is modified script:
while ($row = mysql_fetch_assoc($result)) {
$pos = stripos(strtolower($row['word']), $pattern);
if(!($pos === false)) {
$size ++;
$word = $row['word'];
$response .= "<option value=\"$word\">$word</option>";
}
}
But actually next script will run faster:
if ($result = mysql_query("SELECT word FROM words where word like '%".mysql_real_escape_string($pattern)."%'")) {
$response = "";
$size = 0;
while ($row = mysql_fetch_assoc($result)) {
$size ++;
$word = htmlspecialchars($row['word']);
$response .= "<option value=\"$word\">$word</option>";
}
echo "<select id=\"list\" size=$size onclick=\"selectValue()\">$response</select>";
}
And yes - use mysqli instead of mysql, mysql_ functions are deprecated.

How to fix Header new line error

I am receiving the following error message "Header may not contain more than a single header, new line detected" I know it says that a new line has been detected, but I cannot figure where this line is coming from. I have tried to trim the variables..I have re-written the header line in different ways, without any result. I added the getallheaders function to see what was being passed, but I see no new line or any extra characters in the output $headers. Even using ob_start() does not help.
<?php
ob_start();
include "catalog.obj";
session_start();
$catalogObj = $_SESSION['catalogObj'];
if (isset($_POST['st']))
$st = $_POST['st'];
else
$st = '0';
if (isset($_POST['num']))
$num = $_POST['num'];
else
$num = '0';
if (isset($_POST['type']))
$type = $_POST['type'];
else
$type = '0';
if (isset($_POST['rec']))
$rec = $_POST['rec'];
else
$rec = '0';
if (isset($_POST['option']))
$option = $_POST['option'];
else
$option = '0';
if(strcmp($_POST['submit'],"Reset Form") == 0)
{
header("location: search_catalog.php?type=$type&firstTime=1");
exit;
}
elseif(strcmp($_POST['submit'],"Catalog Administration") == 0)
{
Header("Location: administration.php");
exit;
}
else
{
$inventory_id_num = $_POST['inventory_id_num'];
$inventory_desc = $_POST['inventory_desc'];
$inventory_revision = $_POST['inventory_revision'];
$quantity = $_POST['quantity'];
$catalog_status_id = $_POST['catalog_status_id'];
$order_form_type_id = $_POST['order_form_type_id'];
$catalogObj->inventory_id_num = $inventory_id_num;
$catalogObj->inventory_desc = $inventory_desc;
$catalogObj->inventory_revision = $inventory_revision;
$catalogObj->quantity = $quantity;
$catalogObj->catalog_status_id = $catalog_status_id;
//$catalogObj->order_form_type_id = array();
$catalogObj->order_form_type_id = $order_form_type_id;
$count=count($order_form_type_id);
for ($i=0; $i<$count; $i++)
{
//print "order_form_type_id: $order_form_type_id[$i]<br>";
if(strlen($order_form_type_id[$i]) > 0)
{
$catalogObj->order_form_type_id[$i] = $order_form_type_id[$i];
}
}
if(strcmp($_POST['submit'],"Back to Order Form") == 0)
{
Header("Location: order_form.php?num=$num");
exit;
}
else
{
//$url = "type=".$type."option=".$option."rec=".$rec."st=".$st."num=".$num;
Header("location: search_catalog_handler.php?type=$type&option=$option&rec=$rec&st=$st&num=$num");
//Header("location: search_catalog_handler.php?" . rawurlencode($url));
if (function_exists('getallheaders'))
{
$headers = getallheaders();
print_r( $headers);
}
exit;
}
}
function getallheaders()
{
$headers = '';
foreach ($_SERVER as $name => $value)
{
if (substr($name, 0, 5) == 'HTTP_')
{
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
?>
First, thanks for the pointers! The problem in the above code was with the $st variable. I am not very experienced with headers and rewriting them but I had add the following conditinal statement:
if (!empty($_POST['st']))
{
$st = $_POST['st'];
$num = $_POST['num'];
$type = $_POST['type'];
$rec = $_POST['rec'];
$option = $_POST['option'];
}
To the beginning of my code, so it the complete code is:
<?php
ob_start();
/*************************************
altered complete 12/20/2013
rjm
*************************************/
include "catalog.obj";
session_start();
$catalogObj = $_SESSION['catalogObj'];
if (!empty($_POST['st']))
{
$st = $_POST['st'];
$num = $_POST['num'];
$type = $_POST['type'];
$rec = $_POST['rec'];
$option = $_POST['option'];
}
if(strcmp($_POST['submit'],"Reset Form") == 0)
{
header("location: search_catalog.php?type=$type&firstTime=1");
exit;
}
elseif(strcmp($_POST['submit'],"Catalog Administration") == 0)
{
Header("Location: administration.php");
exit;
}
else
{
echo "<pre>";
print_r($_POST);
echo "</pre>";
//exit;
$inventory_id_num = $_POST['inventory_id_num'];
$inventory_desc = $_POST['inventory_desc'];
$inventory_revision = $_POST['inventory_revision'];
$quantity = $_POST['quantity'];
$catalog_status_id = $_POST['catalog_status_id'];
$order_form_type_id = $_POST['order_form_type_id'];
$catalogObj->inventory_id_num = $inventory_id_num;
$catalogObj->inventory_desc = $inventory_desc;
$catalogObj->inventory_revision = $inventory_revision;
$catalogObj->quantity = $quantity;
$catalogObj->catalog_status_id = $catalog_status_id;
$catalogObj->order_form_type_id = $order_form_type_id;
$count=count($order_form_type_id);
for ($i=0; $i<$count; $i++)
{
if(strlen($order_form_type_id[$i]) > 0)
{
$catalogObj->order_form_type_id[$i] = $order_form_type_id[$i];
}
}
if(strcmp($_POST['submit'],"Back to Order Form") == 0)
{
Header("Location: order_form.php?num=$num");
exit;
}
else
{
Header("location: search_catalog_handler.php?type=$type&option=$option&rec=$rec&st=$st&num=$num");
exit;
}
}
?>
This allows for a specific type search (with parameters) and a general type search (no parameters) from the sending page.
Assuming that catalog.obj does not output any information to the browser (which would result in an error as well), your $type variable looks like the culprit since it's the only wildcard.
Note that you'll need to do the following for all POSTed variables in your script that you want to use in a URI:
Sine it's possible that $type could be anything (it's using the POSTed variable sometimes), you should clean it up before spitting it back out in your header:
$type = urlencode($type); // Prepares the variable to be inserted in the URI
header("Location: search_catalog.php?type=$type&firstTime=1");

How to get the next row in mysqli result?

I want to get the next row in a cycle of a mysql result to write a list with the next and previous element code like this example:
<?php $previous = null;
$next = null;
while ($row = $result->fetch_assoc()) {
$next = ??????????????? // how to get it?
?>
<li code="<?php echo $row['code']; ?>" previous="<?php echo $previous; ?>" next="<?php echo $next; ?>">Hello world!</li>
<?php
$previous = $row['code']; // previous is easy to get...
} ?>
Not sure with MySQLi but here is how I do it in PDO:
$data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
for($i = 0; $i < count($data); $i++) {
$next = isset($data[$i+1]) ? $data[$i+1] : null;
}
if there is no fetchAll in mysqli then do:
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
if ( ! empty($data)) {
for($i = 0; $i < count($data); $i++) {
$next = isset($data[$i+1]) ? $data[$i+1] : null; // next
$prev = isset($data[$i-1]) ? $data[$i-1] : null; // previous
$curr = isset($data[$i]) ? $data[$i] : null; //current
}
}
How about something on the lines of this:
SELECT * FROM table WHERE id > '$currentID' ORDER BY id LIMIT 1;
to get the single next possible row.
Look at this solution, what do you think about it? it works, but will be more slow? this algorithm requires at least double pass or not? what do you think?
$db = DB::getConnection();
$result = $db->query( "SELECT * FROM objects" );
$next = null;
$previous = null;
$temp_previous = null;
$print = false;
$row = null;
$fetchit = false;
$code = null;
echo "<ul>";
while (true)
{
if ($print) {
if ($fetchit) {
$row = $result->fetch_assoc();
$next = $row['code'];
}
echo "<li code='$code' next='$next' previous='$previous'>{$row['description']}</li>" . endl();
$previous = $temp_previous;
$print = false;
}
else {
if (!$fetchit)
$row = $result->fetch_assoc();
$code = $row['code'];
$temp_previous = $row['code'];
$print = true;
$fetchit = true;
}
if(!is_array($row)) break;
}
echo "</ul>";

Categories