Welcome Guest, Not a member yet? Register   Sign In
foreach instead of while in querys
#1

[eluser]Steeep[/eluser]
Well for starters, excuse my bad English, not my native language.

I have the following code, but I get problems when displaying results database & save them to a file.

Code:
$mode = !file_exists(APPPATH . 'config/forum.php') ? 'x' : 'w';
  
  $options = array(
   "order_by"  => "stid",
   "order_dir"  => "ASC"
  );

  $query = $this->db->query('SELECT title, value FROM settings ORDER BY ' .$options['order_by']. ' ' .$options['order_dir']);
  foreach ($query->result_array() as $setting)
  {
   $setting['value'] = addcslashes($setting['value'], '\\"$');
   $settings = "\$config['forum']['{$setting['title']}'] = \"{$setting['value']}\";\n";
   print_r($settings);
  }
  
  $settings = "<"."?php if (!defined('BASEPATH')) exit('No direct script access allowed'); \n\n/* \n| -------------------------------------------------------------------\n| FORUM SETTINGS\n| -------------------------------------------------------------------\n| This file contains personal settings of your forum. These data are generated by the database.\n|\n*/\n\n$settings\n?".">";
  $file = @fopen(APPPATH . "config/forum.php", $mode);
  @fwrite($file, $settings);
  @fclose($file);

It should show the results as follows:

Code:
$config['forum']['closed'] = "1";
$config['forum']['closed_reason'] = "";
$config['forum']['name'] = "ForumCI";
$config['forum']['url'] = "";
$config['forum']['friendly_urls'] = "0";

Pero solo me muestra & guarda solo el último resultado, así:

Code:
$config['forum']['friendly_urls'] = "0";

And if I change the foreach for while, I get errors & saves me the results correctly. The code is this:

Code:
if(!file_exists("config/forum.php"))
{
  $mode = "x";
}
else
{
  $mode = "w";
}

$options = array(
  "order_by" => "stid",
  "order_dir" => "ASC"
);

$query = $db->query('SELECT title, value FROM settings ORDER BY ' .$options['order_by']. ' ' .$options['order_dir']);

while($setting = $db->fetch_array($query))
{
  $setting['value'] = addcslashes($setting['value'], '\\"$');
  $settings = "\$config['forum']['{$setting['title']}'] = \"{$setting['value']}\";\n";
}

$settings = "<"."?php if (!defined('BASEPATH')) exit('No direct script access allowed'); \n\n/* \n| -------------------------------------------------------------------\n| FORUM SETTINGS\n| -------------------------------------------------------------------\n| This file contains personal settings of your forum. These data are generated by the database.\n|\n*/\n\n$settings\n?".">";
$file = @fopen("config/forum.php", $mode);
@fwrite($file, $settings);
@fclose($file);

I can do?
#2

[eluser]NeoArc[/eluser]
The problem is here, you are overwritting $settings every time the loop runs:
Code:
$settings = "\$config['forum']['{$setting['title']}'] = \"{$setting['value']}\";\n";

Just change it to the correct string operator, (and declare $settings before the foreach block)
Code:
$settings .= "\$config['forum']['{$setting['title']}'] = \"{$setting['value']}\";\n";
#3

[eluser]Steeep[/eluser]
Creo que no hay problema en que lo diga en español..

Solo era eso, & crei que borrando el punto sería mejor, pero ya veo que no, gracias Smile




Theme © iAndrew 2016 - Forum software by © MyBB