Looking for a bit of advice.
I have an update script for my website’s database, but as yet - I’ve not been able to figure out how to convert it to the Codeigniter framework ie. it’s saved as it’s own file in the home directory of my website, and executed from there.
There’s a couple of things making it difficult for me to figure out:
It uses php xpath - not difficult to see how I would convert this to a Codeigniter controller, but I’m wondering if update_batch would be a better solution for me as opposed to using a foreach loop as each row is update by just 1 index column. There’s next to no documentation available on this however…
My code utilises MySql’s On duplicate feature which as I understand is not supported in Codeigniter.
Code:
$string = 'http://mywebsiteetc.com/xml/gzip/';
$xml = simplexml_load_file("compress.zlib://$string");
foreach ($xml->xpath('//merchant') as $row)
{
$merchant_id = $row['id'];
$merchant_name = $row['name'];
mysqli_query($con, "INSERT INTO merchants (merchant_id, merchant_name) VALUES ('$merchant_id', '$merchant_name') ON DUPLICATE KEY UPDATE merchant_id = '$merchant_id', merchant_name = '$merchant_name'");
}
Can anyone steer me in the right direction as to how to implement the above into a controller/model environment - and of course, advise me of the most efficient way to do all of the above if there is one?
Cheers!
Basically you need to know more about the MVC structure of Codeigniter, you can start reading here for the controller.
In your setup:
create a file in controller app/controllers/update.php
in your update.php you can write your functions in public function index()
Parse your xml and pass it in Model (create a model for this setup)
then save/inserts it one by one or per batch , put the code in your Model
Again you have to read the documentation for a better understanding how to separate your MVC and make it a one functionality
Related
I just started learning laravel.
I don't know how can I fetch data inside a controller,
I want to use that data inside a controller to make more rules.
//Get Data From Database
$db = DonorDetail::where('donation_id', $paytmResponse['ORDERID'])->get();
$name = $db['donor_name'];
$email_id = $db['email_id'];
DonorDetail is my model to connect to my database.
I tried this code, but getting undefined error.
I'm trying to select only 1 row using the unique donation_id and use data of other columns of data inside my controller.
get returns a collection. You should use first to get the first result from your query:
DonorDetail::where('donation_id', $paytmResponse['ORDERID'])->first();
If no results were found, null will be returned, so you should also pay attention to that.
You only provided us with a small portion of your code, but it seems like you're doing a lot of things in a non-Laravel way. It's only natural because, as you mentioned, you're just starting with Laravel. My recommendation to you is to check out Laracast's free "Laravel From Scratch" course:
https://laracasts.com/series/laravel-6-from-scratch
It's by far the best resource for developers taking their first steps in Laravel (and it also has good resources for experienced developers).
Good luck!
you can use dd() to see the data.
dd($db);
However, If you want to fetch only first matching data, use following
$db = DonorDetail::where('donation_id', $paytmResponse['ORDERID'])->first();
$name = $db->donor_name;
$email_id = $db->email_id;
Care to put light to the subject. Issue is that when I do what ever to code I find that code does not go further to retrieve data from table Sectors and table courses. I have applied find() I use CakePHP 4.x to make clear that it is latest version of CakePHP called Strawberry. I also do not understand that why does my code get affected while I apply simple code structure to run my code.
Take a look at code snippet to run a simple find query with CakePHP Strawberry.
$Sectors is not defined but I need to know a way to define and apply $Sectors variable so that I have given value for relative table structure.
Table Does not load because of a problem I do not understand yet but guys help me out I'll update my code and make use of real code that is to determine a structure for code that will work for CakePHP Strawberry.
code:
$this->loadModel('Sectors');
$this->loadModel('Courses');
$Sectors = $Sectors->find('all')->contain('Sectors', 'Courses');
pr($sectors);
$this->contain(['Sectors', 'Courses']);
$this->set(compact('sectors'));
To retrieve data from table so that I get data relation from Sectors and Courses table.
error:
Call to a member function find() on null
Answer is simple but problem is CakePHP would keep single model with multiple query but not multiple model with single query.
This might sound kind of basic (and it probably is), but I've been trying to get this for a few hours now and either I need a break from coding or it just wont budge.
I have an active record variable which brings database entries from a model:
$variable = Model::model()->findAll();
So I have $variable available in my view file and I want to check for the existence of a specific entry within the results. I am using the primary key of an entry available in the $variable, but I can't seem to get it working.
What is the correct way to check if a given entry is contained within that variable from the view file, not the controller?
PS: I do not want to iterate through the result set, it wouldn't be very efficient for my application.
Thanks.
If I got you right then:
Indeed its better not to have such pieces of code in a view file.
If you are forced to use CActiveRecord.fin*() methods, consider using findByPk($pk). If it returns null - no such record.
Consider having your models extending PcBaseArModel as that class has 'checkExists($pk)' method that looks natural in the check you need to perform. This base class has other goodies - check its documentation.
Try this code:
$data = Post::model()->findAllBySql("select * from tbl_post where id=".$data->id);
or
$post=Post::model()->find(array(
'condition'=>'postID=:postID',
'params'=>array(':postID'=>$data->id)
));
I have an issue I keep hitting the wall on and I need some suggestions on the best way around this.
I got a loop that grabs data from my model, but within that loop, I have to validate it against a library, but I cant seem to find a good way to do that.
for example, I got a model method that looks similar to this:
public function GetUserList() {
$this->db->select('username, email, joindate, rank')->from('user');
$query = $this->db->get();
//loop through data and bind to an array.
foreach ($query->result() as $row) {
$users[] = $row;
}
return $users;
}
when the page loads the user will see a list of users and their info, but some users don't want their email address to be shown, we then have to use the user library to fetch the settings for that user and see if we should hide that email or not.
the only solution i found was to use a helper function as a detour, but I feel that is going to bite me in the future duplicating things like that.
the other issue I found was once you created the library it didn't restart the object as a new one (like it would in vanilla PHP).
I'm hoping someone has a good way to resolve a setback like this.
One last item, if it makes a difference, I'm also using Twig to handle my views.
You do not need to load every library file using the load() function. The load() function is for loading a single instance in the global (CI) namespace. If you want to work with a collection of objects, you should require the file like you normally would in PHP.
I would personally join on your preferences table if possible. That way you wouldn't have to do a separate query for every single user.
Is there a codeigniter plugin that allows me to quickly create find by functions without writing code on every field in a db table?
I find myself writing a lot of functions for tables such as findbyid findbyfirstname findbyemail and so on, any libraries already written to speedup my dev time? i tried googling but i havent come across any.
If you mean you have to write multiple methods in your model to find rows in a table by a specific field, you could just pass an associative array containing the fields and values you want to search to a generic function - something like:
function search_mytable($search=array()) {
$this->db->select('mytable.*');
$this->db->from('mytable');
if(!empty($search)
$this->db->where($search);
}
There's more information about what you can pass the CI active record where method here http://codeigniter.com/user_guide/database/active_record.html#select
If it's just simple data retrieval, you can just do something like this:
function find($column, $value)
{
$this->db->where($column, $value);
//etc
}
for simple queries. As BrynJ suggests, the Active Record class is rather flexible when it comes to taking parameters.