Welcome Guest, Not a member yet? Register   Sign In
Calling a database to make an array string
#1

[eluser]agammill[/eluser]
I'm a codeigniter beginner. I've been going over the tutorial of getting data and after various failed attempts, I've come here.

I currently use str_replace with an array but because the array is so large and is used over in various places, I wanted to put the elements in a table.

So this is what works: (I shortened the array since there are over 400 elements),

In Controller
Code:
$tag_input = str_replace(
  array('text1','text2','text3'),
  array('ő','ì','č'),
$tag_input
);

This is my latest failed attempt. I'm not getting errors but the str_replace isn't replacing.

Model
Code:
<?php

class Character_words_model extends MY_Model {

// vars
public $char_word;
public $word;

/**
  * constructor
  */
function __construct() {
  parent::__construct();
  
  // initialize
  $this->setTable('char_word_ref');
  $this->setOrderBy('length', 'desc');
}
}
?>


In Controller:
Code:
function __construct() {
  parent::__construct();
  
  // make sure it's an ajax call
  if (!IS_AJAX) {
   die('this is not an ajax call');
  }
  
  $this->load->model('character_words_model');

}


$query = $this->db->query("SELECT * FROM  'char_word_ref'"); //I know I'm calling the query
//again but nothing else was working, including this.

$tag_input = str_replace(
  foreach ($query->result() as $row) {$row->word;},
  foreach ($query->result() as $row) {$row->char_word;},
  $tag_input
);

What am I missing?

Thanks for any help you can give me.
#2

[eluser]PhilTem[/eluser]
You're using the foreach loop in the wrong way: the loop needs to be outside of your data processing not inside.

I tried fixing your code but I'm not able to, because I don't know where the initial tag_input comes from. Would however be something like this

Code:
foreach ( $query->result() as $row )
{
  $tag_input = str_replace($row->word, $row->char_word, $tag_input);
}
which will replace every single instance of your word-column with the corresponding value in the char_word-column.
#3

[eluser]agammill[/eluser]
Sorry about that. $tag_input is ONE text string. Basically I search the entire database for pieces inside the text string, reducing it. So if it finds the word 'one', it changes it to the numeral 1, etc...

And I tried what you said, AND IT WORKED!!!!!!

Thanks, PhilTem. I've been working on this for two days.





Theme © iAndrew 2016 - Forum software by © MyBB