Trying to get property of non-object - CodeIgniter - php

I'm trying to make update form, that is going to retrieve the data for the specific ID selected, and fill in the form, so its going to be available for updating.
When I click edit on the specific entry (Product in my case), it takes me to the edit_product_view, but states the error "Trying to get property of non-object" for every variable that I use in set_values of the form elements.
Using print_r, I get the correct associative array, so it's passed correctly.
This is excerpt of my edit_product_view.
<h2><?php echo $heading; ?></h2>
<hr>
<table id="newproduct">
<?php echo form_open('products/edit/'.$product->id); ?>
<tr>
<td class="label"><?php echo form_label('Name:');?></td>
<td><?php echo form_input('prodname', set_value('prodname', $product->prodname));?></td>
</tr>
<tr>
<td class="label"><?php echo form_label('Product Type:');?></td>
<td><?php echo form_dropdown('ptname_fk', $product_types, set_value('ptname_fk', $product->ptname_fk));?></td>
</tr>
$product is the array holding all the key-value pairs, but I cannot fill the form for some reason.
Thank you in advance!

To access the elements in the array, use array notation: $product['prodname']
$product->prodname is object notation, which can only be used to access object attributes and methods.

To get the value:
$query = $this->db->query("YOUR QUERY");
Then, for single row from(in controller):
$query1 = $query->row();
$data['product'] = $query1;
In view, you can use your own code (above code)

In my case, I was looping through a series of objects from an XML file, but some of the instances apparently were not objects which was causing the error. Checking if the object was empty before processing it fixed the problem.
In other words, without checking if the object was empty, the script would error out on any empty object with the error as given below.
Trying to get property of non-object
For Example:
if (!empty($this->xml_data->thing1->thing2))
{
foreach ($this->xml_data->thing1->thing2 as $thing)
{
}
}

Related

Getting id from a textarea in a while loop in PHP for updating in mysql

I have a table where is one week displayed (each row is one day).
I get the rows from a while loop from my database. The rows are displayed in bootstrap accordions.
There is a textarea in every accordion row where the user can input (update) some text.
I want to update this text into my database. It should update the text depending on the day id.
<form method="POST" action="">
<table class="table table-hover" style="border-collapse:collapse;">
<thead>
<tr>
<th>Weekday</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php
// Select Statement (for shortening not included into this Stack question)//
while($row = $statement->fetch()) {
$thedate = $row['Date'];
$weekday=strftime("%A", strtotime($thedate));
$date=date('d-m-Y', strtotime($thedate));
echo "<tr data-toggle='collapse' data-target=#".$row['Date']." class='clickable collapse-row collapsed'>";
echo "<td >".$weekday."</td>";
echo "<td>".$date."</td>";
echo" <td style='color:black; font-size:20px;'><i class='fas fa-angle-down'></i></td>";
echo "</tr>";
echo "<tr><div class='accordian-body collapse' id=".$row['Date'].">
<td colspan='1' class='hiddenRow'><textarea name=".$row['id']." rows='5' cols='80'>".$row['Text']." </textarea></td>
//the $row['id'] should give every textarea a unique dayid from my database
echo"</td>
</div></tr>";
}
if(ISSET($_POST['id'])){
$debug=$_POST['id'];
}
var_dump($debug); // var_dump for debugging. See text below
?>
</tbody>
</table>
<button type="submit" name="Speichern" class="btn btn-lg btn-primary btn-block">Speichern</button>
</form>
Before writing the Sql Update Statement I wanted to debug to find possible bugs.
If i debug this with var_dump I get the error message "Undefined variable $debug" and I dont know why. The variable shouldnt be empty because in the textareas is always text.
Im new to PHP and coding at all so probably Im making a dump mistake.
EDIT: If I put the var_dump inside the if condition i get nothing as return.
I tried it also with the var_dump in the if block but then i get nothing as return.
That’s because you do not have any form field that is actually named id. You put name=".$row['id']." on your textarea, and that is likely a numeric value. And you probably don’t know which one that will be, on the receiving end.
Plus, since you are creating multiple such fields in a loop, PHP will overwrite all values for this parameter with the last one. You need to use a naming scheme that includes square brackets to avoid that, something like name="foo[]" - then $_POST['foo'] will become an array that you can loop over.
And since you will still need your record ID to associate with the data, you can put that into the brackets, name="foo[123]" – then this 123 will become the key of that array element, for this specific textarea.
If you loop over that using the extended foreach syntax, then you have easy access to the ID, and the value entered by the user:
foreach( $_POST['foo'] as $id => $value ) { … }

Trouble getting a value from <a> link - PHP

My problem is that I need to save a value from the link as a variable which will then do something. Below is my main code for this problem.
<table>
<? foreach ($csv as $row) : ?>
<tr>
<td><? echo $row[0]; ?></td>
</tr>
<? endforeach; ?>
</table>
Basically, it prints the first column from my array $csv. However I want to save the '$row[0]' for each link - depending on which one is clicked.
This happens here:
<?php
if (isset($_GET['GoToProfile'])) {
}
?>
This works. E.g. when something is clicked it prints something. But I cannot find a way to save the values from each link. Depending on which one is clicked. I have tried many different methods online, but none seem to work.
I have even tried:
<? echo $row[0]; ?>
Any help would be greatly appreciated.
Thanks!
Use an ampersand (&) instead of a question mark
<? echo $row[0]; ?>
The ? indicates the beginning of the query string, which is the data sent on a GET request. In most cases it is a collection of name/value pairs, separated with & s.
A simple example of a GET request
http://example.com?first=1&second=fifty
You would get the value of the parameters in PHP with $_GET
$first = $_GET['first'];
$second = $_GET['second'];
To see what the server is receiving, you can use var_dump
var_dump($_GET)

Passing variable to a controller in CodeIgniter

Hello I am displaying data in the view but each time that i display i want to store the Student_id in a variable the pass that variable to a controller but it is giving me errors
foreach($results as $row)
{
$this->load->helper('url');
echo '<a href="http://localhost/amref/mama/get_student?var1=<?php echo $row->student_id;?>" '.'<tr><td>'.$row->student_fname.' '.$row->student_lname.'</td><td>'.$row->gender.'</td><td>'. $row->res_district.' , '.$row->res_region.'</td><td>'.$row->collage_name.'</td><td>'.$row->course_name.'</td></tr>'.'</a>';
}
Your syntax is incorrect and I am pretty sure you want that anchor tag nested inside a <td> element, maybe like this:
echo '<tr><td>'.$row->student_fname.' '.$row->student_lname.'</td><td>'.$row->gender.'</td><td>'. $row->res_district.' , '.$row->res_region.'</td><td>'.$row->collage_name.'</td><td>'.$row->course_name.'</td></tr>';

How do I pass rows from HTML form to PHP?

I'm written a PHP program to display a tab delimited file. The purpose of this is to allow the user to views the rows and select which ones they want by checking the checkbox given in the row for each record. After they hit submit I have a PHP program to display the values, but the problem is only the last row's ID is being passed. However, when the user hits the SUBMIT button I can see all the values for the rows checked:
process_form.php?download=5108&download=5110&download=5114
How should I parse this in process_form.php? I've done a var_dump of $_POST and also
$_REQUEST but it only shows the last value which is 5114. I kind of understand the problem, most of the time in forms programmers only get one value per input field, but what happens when there are many records? It doesn't seem they should all have their own unique 'name'.
<td align=center><input type="checkbox" name="download" value="<?php echo $row['ID']; ?>"></td>
I'm doing something wrong here, but I'm not sure what. Is there a way to pass an array (I'm guessing) of IDs? Or should I be looking at parsing the URL of ?download=5108&download=5110&download=5114 because it has all the values I need there? If so, how do I do that? Thanks!
This is my solution:
<td align=center><input type="checkbox" name="download[]" value="<?php echo $row['ID']; ?>"></td>
Notice that download is now download[], thus creating an array to be passed to the PHP program to process the form.
Then using this demo PHP code I was able to get access to the array:
$my_array = ($_REQUEST["download"]);
print_r($my_array);
echo "<P>";
foreach ($my_array as $value)
{
echo $value . "<BR>";
}
I'd rather use your rowID to identify the name of <input> instead of the value.
<td align=center><input type="checkbox" name="download_<?php echo $row['ID']; ?>" value="1"></td>
Then you can process your request array like:
foreach( $_REQUEST as $key => $value ) {
if( preg_match('/^download_([0-9]+)$/', $key, $reg ) {
$rowId = $reg[1]; // Your row ID
$isChecked = $value; // State of checkbox
}
}
The row ID is parsed from variable name using regexp.
EDIT:
As mentioned in comments, this is not the simplest way to read an array of checkboxes. The simpliest is to name checkboxes download[] and parse this array in PHP then.
However, this is more universal, for example when you need to get array of input texts instead of checkboxes.

Codeigniter Calling Controller with data from a form issue

Trying to call the projects controller and the editproject function and pass an id number. Can anyone tell me why the second line doesn't work? When I echo the value in the first line, it does give me the correct integer as a string
<?php echo $list[0]->id; ?>
<?php echo form_open('projects/editproject/',$list[0]->id ) ;?>
The error I keep getting is "Missing argument 1 for Projects::editproject()" My editproject function is function editproject($id).
I did try:
<?php echo $list[0]->id; ?>
<?php $pdata = (int)$list[0]->id; ?>
<?php echo form_open('projects/editproject/',$pdata ) ;?>
Thinking the call to the controller needed a variable for the data. Same error message as above. THanks for any help.
Did you mean to do this instead ?
<?php echo form_open('projects/editproject/'.$list[0]->id ) ;?>
The second argument of form_open() accepts an associative array of attributes, hence, you're mistakenly passing in the id into the second argument when it needs to be concatenated with the url instead.

Categories