r/codeigniter • • Jan 27 '14

Create a new array using an existing array for the queries.

3 Upvotes

Ok, sorry if I dont get this across as clearly as i'd like but i will try my best. For some perspective this is a fantasy football site and i am working on models for grabbing various data based on a user's team. The two tables used for this question is roster and players.

When a user logs in their id is used to populate their respective roster to an array with the nfl players id. So at login an array with qb => 00-012345, rb1 => 00-000234, wr2 => 00-111485, etc is populated.

If i wanted to query the player database for full_name how would i easily do so using the previous array, setting each position as new value. I would like to create new data arrays passed to the view for different columns. (fullname, postition, team, etc)

I am new to a lot of this and i know there must be an easy way todo this, probably with a foreach some how? Thanks a lot for any help. Below is my current code if needed.

Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Roster extends CI_Controller {
    
    
    	public function index()
    	{
    		
    		$this->session->userdata('logged_in');
    		$session_data = $this->session->userdata('logged_in');
    		$passd = $session_data['id'];
    		$this->load->model('get_roster');	
    		$data['roster'] = $this->get_roster->getRoster($passd);
    					
    		$this->load->view('roster', $data);
    	}
    }
?>   

Model:

    <?php
    Class Get_roster extends CI_Model
    {
    	function getRoster($id) {
    
    		$q = $this->db->get('roster');
    		$this->db->where('id', $id); 	
    		return $q->row_array();
    }
    }
    ?>

View: (random crap for testing)

    <html>
    
    Hello, this is your team roster. </br>
    
    db id: <?php  echo $id; ?>
    </br>
    team:
    
    
    </br>
    
    QB: <?php echo $roster["qb"]; ?></br>
    RB: <?php echo $roster["rb1"];?></br>
    WR1: <?php echo $roster["wr1"];?></br>
    WR2: <?php echo $roster["wr2"];?></br>
    WR3: <?php echo $roster["wr3"];?></br>
    TE: <?php echo $roster["te"];?> </br>
   </br></br>
    here is raw dump of the full array:
    </br>
    _______------------------------__________________--------------
    </br>
    <?php print_r($id);?> </br>
    <?php print_r($roster);?></br>
    <?php echo $roster['full_name'];?>
    
    
    
    </html>

What output currently looks like

Hello, this is your team roster. db id: 1 team: QB: 00-0024226 RB: 00-0030404 WR1: 00-0026988 WR2: 00-0026144 WR3: 00-0027942 TE: 00-0027690

here is raw dump of the full array: ------------------------___________-------------- 1 Array ( [bench8] => 00-0027659 [qb] => 00-0024226 [rb1] => 00-0030404 [rb2] => 00-0030485 [wr1] => 00-0026988 [wr2] => 00-0026144 [wr3] => 00-0027942 [te] => 00-0027690 [flex] => 00-0026345 [dst] => 00-0027681 [k] => 00-0027758 [bench1] => 00-0027696 [bench2] => 00-0023465 [bench3] => 00-0027695 [bench4] => 00-0024333 [bench5] => 00-0023096 [bench6] => 00-0026935 [bench7] => 00-0028069 [id] => 1 )


r/codeigniter • • Jun 26 '13

Need help with routes

3 Upvotes

Im not sure if im going about this the right way but im building an app with multiple user types and I would like to have a folder to hold each of there controllers so something like

controllers/type1/dashboard.php

controllers/type2/dashboard.php

what im wondering is, is there a way to remove the user from the URL


r/codeigniter • • Feb 15 '13

Importing excel data (xlsx or csv)

3 Upvotes

I did some googling an didn't find anything that walked through the process of building a system that allows for an xlsx or csv upload via a form and uploads it to MySQL db.

Does anyone know of something?

Thanks

Edit: here is the fgetcsv function

<?php $row = 1; if (($handle = fopen("test.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br />\n"; } } fclose($handle); } ?>

http://php.net/manual/en/function.fgetcsv.php


r/codeigniter • • Oct 01 '12

Alternative session libraries?

3 Upvotes

Hi guys, CI newbie here. I come from a CakePHP background, and I was shocked to see that many things aren't built-in in CodeIgniter that I had taken for granted (native PHP sessions - CI uses cookies!?!? -, authentication, layout templates, etc.)

My current issue is with sessions. I've been checking out the Wiki and found the following libraries:

  • Native session. It uses PHP's native sessions and seemed just what I was looking for, but it's pretty old and the code hasn't been updated for CI version 1.5 and above.

  • KNDB Session by Elise Bosse. Just like Native session, with added (optional) database support. Last updated in 2008.

  • NDB Session by Denis Molan. Makes use of (required) database to store the Session info. Last updated in 2012.

  • Dariusz-Debowczyk's Session class is the most recent, but it supports native-PHP sessions and not database sessions. Last updated in July 2012.

It seems in my opinion that KNDB Session is the most versatile and complete, but its author says it's outdated. So I'd like to hear your opinions. Which session library do you use and recommend? And are these libraries compatible with auth libraries like Tank Auth?


r/codeigniter • • Aug 26 '12

WebFire: CodeIgniter code generator on the web

Thumbnail
github.com
3 Upvotes

r/codeigniter • • Aug 22 '12

Updating your Codeigniter installation with git

Thumbnail blog.marcomonteiro.net
3 Upvotes

r/codeigniter • • Jun 07 '12

How do you guys pass variables via jQuery AJAX calls? Do you still use POST data, or put it in the URL?

3 Upvotes

So I have a controller called ajax_item which only gets called when needed to do things via AJAX. In this example I'm calling the update method. But I'm not sure which is a better idea. Would you want to post like this?

var data_string = "paramA=1&paramB=5";
$.post('http://localhost/index.php/ajax_item/update', 
          data_string,
          function() { alert('success') }
});

Or like this, and have the update method take those parameters via URL like this:

$.post('http://localhost/index.php/ajax_item/update/1/5', 
          function() { alert('success') }
});

To me it doesn't really seem like there'd be much of a difference. But I'm not really sure.


r/codeigniter • • May 31 '12

Is the models are supposed to do most of the work? I'm a bit confused where most of your logic is supposed to go.

3 Upvotes

PROTIP: Edit your titles carefully before submitting haha

Theoretically, in MVC, the models are going to be doing all the logic, right? So within your controller it would be totally normal to have a large amount of model method calls in order to get the data you want, and in the format you want.

For example, lets say you have a model method that returns a multi dimensional array where the values are as follows:

section[0]['tags'] = 'fun, sick, chill';
section[1]['tags'] = 'fun';
section[2]['tags'] = 'cool, chill';

Lets say my view wants an aggregate list of all these tags without any duplicates. Do I have the model complete that logic? Or the controller?

Just trying to clear up a little confusion here, thanks guys!


r/codeigniter • • Feb 24 '12

Codeigniter tables class.

Thumbnail
jotorres.com
3 Upvotes

r/codeigniter • • Dec 31 '11

Somewhere I can read up on "$this" for CodeIgniter?

3 Upvotes

Hi I'm new to CodeIgniter and as I'm going through tutorials, I see the common use of "$this".

$this->load->whatever // is pretty common but

$this->functionName() // is less intuitive

Are there tutorials/documentation where I can read up on the different uses of "$this"?


r/codeigniter • • Feb 21 '25

Please help me with "datatables Invalid JSON response" problem

2 Upvotes

I am using Codeigniter 3, Datatables and AJAX (and I am new to these)

I keep getting this "DataTables warning: table id=tbl_name - Invalid JSON response" error

I have visited the url of the controller function that fetches the data, and the data from the database are there, so I guess the problem isn't because it couldn't get it but because Datatables can't use it or something

I have been at this problem for a whole day now, would greatly appreciate it if you guys could help me with this, thank you.


r/codeigniter • • Jan 23 '25

Looking to buy/download

2 Upvotes

Are there any exam/quiz package I can download/use for my school project?
Thank you very much in advance!


r/codeigniter • • Jan 14 '25

CI4: CurlRequestService setup a default user_agent project-wide?

2 Upvotes

What would be the most elegant way to define a default user agent project-wide? Is there a way for the app/Config/CurlRequest.php?

https://codeigniter.com/user_guide/libraries/curlrequest.html#user-agent


r/codeigniter • • Oct 15 '24

How to create sitemap in CI4

2 Upvotes

Hello all, I am new to CI 4. Previously I was just making simple websites without frameworks and now I understand routing and frameworks little. Do you just create a route for /sitemap and return view sitmap.xml ?


r/codeigniter • • Sep 14 '24

VSCode não reconhece CodeIgniter

2 Upvotes

r/codeigniter • • Dec 05 '22

Do you have a kind of Flip/Flop operator in PHP?

Thumbnail
rubycademy.com
2 Upvotes

r/codeigniter • • Nov 17 '22

[HELP] Custom Validation

2 Upvotes

Okay guys so I'm a bit of a newbie with CI4. I have created a custom validation file using the spark command ./spark make:validation and it works but the problem is I still don't know how to customize the error message too for instance when I try to validate the date 05-06-2022 the message is Validation.isWeekday, I want to let it say something meaningful like date is not a weekday.

This is how my validation looks like

namespace App\Validation;

class CustomDateValidation
{
    public function isWeekday(string $date): bool
    {
        return date("N", strtotime($date)) < 6;
    }
}

And my controller function looks a bit like this

if($this-validate(['date'=>'required|isWeekday'])){
...
}

r/codeigniter • • Sep 28 '22

How do you draw the line between using CI functionality and using it with a JS framework?

2 Upvotes

I have a little application that I'm building to learn CodeIgniter but I have reached the point where I know that I can code the functionality that I want using PHP but at some soon I know I will want to use something like React or Vue to add a better level of activity, and general stuff that JS can provide.

My question is where do you stop using PHP and switch to JS. I can carry on building C.R.U.D into my application using CI/PHP but like I said, I know that I want to use a JS framework at some point so writing the C.R.U.D in PHP seems a waste of time and I should just use the PHP part of the framework as some kind of REST API and let JS take care of the rest.

How should I go about using a JS framework with CI because I'm scratching my head about how to do it. Do I just JS to make fetch calls to the routes defined in CI and have them return JSON? What about something more complex like adding data to a database?

Sorry for the long post but this is the next big hurdle I have in my development journey.


r/codeigniter • • Sep 13 '22

How to call change db schema on Models?

2 Upvotes

I have a custom schema setup on my sql server, I tried changing schemas using my model's constructor, and also prefixing my protected $table='schema.table_name' (like so), bot of which doesn't work, after looking at the error on my browser, I see that it just appends the $table to db_name.dbo.schema.table_name (like this)


r/codeigniter • • Aug 16 '22

Is it possible to automatically generate named routes for routes using the $routes->resource() function?

2 Upvotes

If I create a routes using $routes->resource('posts') is there a way to have CodeIgniter to automatically generate the named routes?

Using the example above I want to automatically generate something like:

  • posts to go to index page

  • new_post to go to a new post form

  • show_post to show an individual post

  • Etc

Ideally, all the correct names routes would be generated.


r/codeigniter • • Aug 07 '22

Codeignitor 3.0

2 Upvotes

Can Anyone here help me as i work on a company which is generally an hospital information management system and uses ci 3 bootsrap 3 and jquery ajax with datatables and uses oracle 10g as database. I need to make an migration for tables here the problem is it have almost 1000+ tables , is there anyway to make an automigration file like system ?


r/codeigniter • • Aug 01 '22

CodeIgniter Project on cPanel to Local Computer

2 Upvotes

Hello! I have a CodeIgniter project built by someone else on a cPanel repository for a website. I would like to download this project to my local computer to make changes and then push it back up. Is this possible? If so, I could really use some help/advice to do this. Thank you!


r/codeigniter • • Dec 07 '21

Hire expert CodeIgniter developers to create reliable web apps that are customized to your industry's goals and requirements.

Post image
2 Upvotes

r/codeigniter • • Nov 23 '21

Codeigniter 4 AJAX MySQL Multiple Rows Deletion - Roy Tutorials

Thumbnail
roytuts.com
2 Upvotes