Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Undefined variable w Codeigniter
piotrek2396
post
Post #1





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 25.09.2013

Ostrzeżenie: (0%)
-----


Mam problem ze zmienną
w modelu daje

  1. public function count_pages()
  2. {
  3. return $this->db->count_all("pages");
  4. }

w controlerze

  1. public function index()
  2. {
  3. $data['id'] = $this->admin_m->count_pages();
  4.  
  5. echo($data['id']);
  6. }


i po tym zlicza mi ładnie, lecz jeśli przekazuje zmienną do widoku

  1. public function index()
  2. {
  3. $data['id'] = $this->admin_m->count_pages();
  4.  
  5. $this->load->view('admin/include/header', $data);
  6. }


a w widoku

  1. <?php echo $id; ?>


to wyświetla mi Undefined variable

Ten post edytował piotrek2396 28.04.2016, 14:47:48
Go to the top of the page
+Quote Post
2 Stron V   1 2 >  
Start new topic
Odpowiedzi (1 - 22)
aras785
post
Post #2





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

Ostrzeżenie: (0%)
-----


"Undefined variable" jaka zmienna?

Zadeklaruj $data:

  1. public function index()
  2. {
  3. $data = array();
  4. $data['id'] = $this->admin_m->count_pages();
  5.  
  6. $this->load->view('admin/include/header', $data);
  7. }
Go to the top of the page
+Quote Post
piotrek2396
post
Post #3





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 25.09.2013

Ostrzeżenie: (0%)
-----


Nie działa niestety, jakiejkolwiek zmiennej bym nie przekazywał to wyskakuje błąd nawet typu

  1. $data['id'] = 'Nagłówek';


model cały

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Admin_m extends CI_Model {
  4.  
  5. public $variable;
  6.  
  7. public function __construct()
  8. {
  9. parent::__construct();
  10.  
  11. }
  12.  
  13. public function get_userID($email)
  14. {
  15.  
  16. $this->db->where('email',$email);
  17. $query = $this->db->get('users');
  18. foreach ($query->result() as $row)
  19. {
  20. $name = $row->name;
  21. }
  22. return $name;
  23. }
  24.  
  25. public function count_pages()
  26. {
  27. return $this->db->count_all("pages");
  28. }
  29.  
  30. }


controler caly

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Countpage extends CI_Controller {
  4.  
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->load->model('admin/admin_m');
  9. }
  10.  
  11. public function index()
  12. {
  13. $data['id'] = $this->admin_m->count_pages();
  14. $this->load->view('admin/include/header', $data, TRUE);
  15. }
  16.  
  17.  
  18. }



widok

  1. <?php echo $id;?>
Go to the top of the page
+Quote Post
aras785
post
Post #4





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

Ostrzeżenie: (0%)
-----


Zapodaj screen błędu lub całą jego treść
Go to the top of the page
+Quote Post
Pyton_000
post
Post #5





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


var_dump($data); w controller.
Go to the top of the page
+Quote Post
aras785
post
Post #6





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

Ostrzeżenie: (0%)
-----


Cytat(Pyton_000 @ 28.04.2016, 16:10:30 ) *
var_dump($data); w controller.


Wyżej napisał, że w kontrolerze mu wyświetla:

  1. public function index()
  2. {
  3. $data['id'] = $this->admin_m->count_pages();
  4.  
  5. echo($data['id']);
  6. }


według mnie, podaje zła ścieżke do widoku lub edytuje inny widok niż powinnie (IMG:style_emoticons/default/biggrin.gif)
Go to the top of the page
+Quote Post
Pyton_000
post
Post #7





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


To co mu się wyświetla a to co zawiera zmienna to 2 różne rzeczy (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post
piotrek2396
post
Post #8





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 25.09.2013

Ostrzeżenie: (0%)
-----


Niestety chciałbym edytować inny widok, lecz niestety dobry. To jakaś głupota zapewne ale już oświałem (IMG:style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post
Pyton_000
post
Post #9





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Kurna znowu nikt nie czyta moich postów ? Co za ludzie...
Go to the top of the page
+Quote Post
piotrek2396
post
Post #10





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 25.09.2013

Ostrzeżenie: (0%)
-----


Czytam czytam, var dump działa , pisałem o tym wyżej.

nawet nie chce mi takiej zmiennej wyrzucić z controlera $data['heading'] = "cos tam";

widok

<?php echo $heading;?>

var_dump($data) wyrzuca mi array(1) { ["id"]=> int(3) }

czyli echo $data['id'] daje 3

czyli w widoku echo $id; powinno wyrzucać 3 a wyrzuca ze nie zdefiniowana

Go to the top of the page
+Quote Post
Pyton_000
post
Post #11





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


pokaż plik 'admin/include/header.php'
Go to the top of the page
+Quote Post
piotrek2396
post
Post #12





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 25.09.2013

Ostrzeżenie: (0%)
-----


  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5.  
  6. <meta charset='UTF-8'>
  7. <meta name="robots" content="noindex">
  8. <meta name="viewport" content="width=device-width, initial-scale=1">
  9. <title></title>
  10.  
  11. <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">
  12. <link href="<?php echo base_url(); ?>inc/css/admin.css" rel="stylesheet">
  13. <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  14. <link href="<?php echo base_url(); ?>inc/css/jquery.bdt.css" type="text/css" rel="stylesheet">
  15.  
  16. <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
  17. <link href='//fonts.googleapis.com/css?family=Lato:400,100,300,700,900&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
  18. <link rel="icon" type="image/png" sizes="16x16" href="<?php echo base_url(); ?>inc/images/favicon.png">
  19.  
  20. <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  21. <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  22. <!--[if lt IE 9]>
  23. <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  24. <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
  25. <![endif]-->
  26.  
  27. </head>
  28.  
  29. <body>
  30.  
  31. <div id="wrapper">
  32.  
  33. <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
  34. <div class="navbar-header">
  35. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
  36. <span class="sr-only">Toggle navigation</span>
  37. <span class="icon-bar"></span>
  38. <span class="icon-bar"></span>
  39. <span class="icon-bar"></span>
  40. </button>
  41. <?php echo anchor(site_url(), '<img src="http://localhost/cms/inc/images/logocms.png">', 'class="navbar-brand"'); ?>
  42. </div>
  43.  
  44. <!-- Top Menu Items -->
  45. <ul class="nav navbar-right top-nav">
  46. <li class="dropdown">
  47. <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-user"></i> <?php echo $this->session->userdata('name');?> <b class="caret"></b></a>
  48. <ul class="dropdown-menu">
  49. <li>
  50. <a href="#"><i class="fa fa-fw fa-gear"></i> Settings</a>
  51. </li>
  52. <li class="divider"></li>
  53. <?php if($loggedin == TRUE): ?>
  54. <li>
  55. <?php echo anchor('admin/panel/logout', '<i class="fa fa-fw fa-power-off"></i> Wyloguj'); ?>
  56. </li>
  57. <?php endif ?>
  58. </ul>
  59. </li>
  60. </ul>
  61. <!-- Sidebar Menu Items -->
  62. <div class="collapse navbar-collapse navbar-ex1-collapse">
  63. <ul class="nav navbar-nav side-nav">
  64. <?php if($loggedin == TRUE): ?>
  65. <li>
  66. <?php echo anchor('admin/panel', '<i class="fa fa-fw fa-dashboard"></i> Dashboard'); ?>
  67. </li>
  68. <li>
  69.  
  70. </li>
  71. <li>
  72. <a href="java script:;" data-toggle="collapse" data-target="#users"><i class="fa fa-fw fa fa-users"></i> Użytkownicy <i class="fa fa-fw fa-caret-down"></i></a>
  73. <ul id="users" class="collapse">
  74. <li>
  75. <?php echo anchor('admin/users', '<i class="fa fa-list-ul" aria-hidden="true"></i> Lista użytkowników'); ?>
  76. </li>
  77. <li>
  78. <?php echo anchor('admin/users/create', '<i class="fa fa-plus-square" aria-hidden="true"></i> Dodaj użytkownika'); ?>
  79. </li>
  80. </ul>
  81. </li>
  82. <li>
  83. <a href="java script:;" data-toggle="collapse" data-target="#pages"><i class="fa fa-fw fa-files-o"></i> Strony <i class="fa fa-fw fa-caret-down"></i></a>
  84. <ul id="pages" class="collapse">
  85. <li>
  86. <?php echo anchor('admin/pages', '<i class="fa fa-list-ul" aria-hidden="true"></i> Lista stron'); ?>
  87. </li>
  88. <li>
  89. <?php echo anchor('admin/pages/create', '<i class="fa fa-plus-square" aria-hidden="true"></i> Dodaj stronę'); ?>
  90. </li>
  91. </ul>
  92. </li>
  93. <li>
  94. <a href="java script:;" data-toggle="collapse" data-target="#articles"><i class="fa fa-fw fa-thumb-tack"></i> Wpisy <i class="fa fa-fw fa-caret-down"></i></a>
  95. <ul id="articles" class="collapse">
  96. <li>
  97. <?php echo anchor('admin/blog', '<i class="fa fa-tag" aria-hidden="true"></i> Dodaj kategorię'); ?>
  98. </li>
  99. <li>
  100. <?php echo anchor('admin/blog', '<i class="fa fa-align-left" aria-hidden="true"></i> Lista kategorii'); ?>
  101. </li>
  102. <li>
  103. <?php echo anchor('admin/blog', '<i class="fa fa-list-ul" aria-hidden="true"></i> Lista artykułów'); ?>
  104. </li>
  105. <li>
  106. <?php echo anchor('admin/blog/create', '<i class="fa fa-plus-square" aria-hidden="true"></i> Dodaj artykuł'); ?>
  107. </li>
  108. </ul>
  109. </li>
  110. <li>
  111. <?php echo anchor('admin/gallery', '<i class="fa fa-fw fa-picture-o"></i> Galeria'); ?>
  112. </li>
  113. <li>
  114. <a href="bootstrap-grid.html"><i class="fa fa-fw fa-lock"></i> Uprawnienia</a>
  115. </li>
  116. <?php endif ?>
  117. </ul>
  118. </div>
  119. <!-- /.navbar-collapse -->
  120. </nav>
  121.  
  122. <div id="page-wrapper">
  123.  
  124. <div class="container-fluid">
  125.  
  126. <?php echo $id;?>
Go to the top of the page
+Quote Post
Pyton_000
post
Post #13





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Ok, no to teraz wrzuć sobie do widoku na koniec "get_defined_vars()" i przeanalizuj co tam masz. Będzie tego spooro pewnie ale cóż.

Btw. Cache wyczyściłeś ?
Go to the top of the page
+Quote Post
piotrek2396
post
Post #14





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 25.09.2013

Ostrzeżenie: (0%)
-----


cache wyczyszczone, brak zmiennej id, data tez nie przekazuje
Go to the top of the page
+Quote Post
aras785
post
Post #15





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

Ostrzeżenie: (0%)
-----


Zrób tak:

W controlerze daj: echo $data['id'];
Następnie w widoku dopisz na samej górze "testowanie" (przed html).
Zrób nam screena i podeślij tutaj.

Ten post edytował aras785 29.04.2016, 12:57:44
Go to the top of the page
+Quote Post
piotrek2396
post
Post #16





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 25.09.2013

Ostrzeżenie: (0%)
-----


http://s10.ifotos.pl/img/widokjpg_ssqnhwp.jpg
Go to the top of the page
+Quote Post
aras785
post
Post #17





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

Ostrzeżenie: (0%)
-----


Screena od samej góry tak aby było widać napis: testowanie oraz echo z kontrolera.
Go to the top of the page
+Quote Post
piotrek2396
post
Post #18





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 25.09.2013

Ostrzeżenie: (0%)
-----


http://ifotos.pl/z/sapepsh
Go to the top of the page
+Quote Post
Pyton_000
post
Post #19





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


echo miałes dać w controller a nie w widoku (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post
rad11
post
Post #20





Grupa: Zarejestrowani
Postów: 1 270
Pomógł: 184
Dołączył: 7.10.2012
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


A dlaczego endif nie kończysz średnikami ? Spróbuj na sztywno wpisać $data["id"] = 1 i zobacz co się stanie i pozamykaj endif średnikami.

Ten post edytował rad11 4.05.2016, 08:15:43
Go to the top of the page
+Quote Post
Pyton_000
post
Post #21





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


@rad11 bo nie ma potrzeby w tym wypadku kończenie ;

  1. <?php
  2.  
  3. $a = 1;
  4.  
  5. if($a == 2):
  6. echo '1';
  7. else:?>
  8. echo '2';
  9. <?php endif ?>
Go to the top of the page
+Quote Post
piotrek2396
post
Post #22





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 25.09.2013

Ostrzeżenie: (0%)
-----


pozamykane, ciągle Undefined variable: id

w controlerze jak pisałem wcześniej poprawnie wywala wynik, ale nie przenosi zmiennej do widoku.

pozamykane endif-y lecz ciągle to samo
Go to the top of the page
+Quote Post
com
post
Post #23





Grupa: Zarejestrowani
Postów: 3 034
Pomógł: 366
Dołączył: 24.05.2012

Ostrzeżenie: (0%)
-----


Wywal te true z końca i czytaj manual uważnie (IMG:style_emoticons/default/wink.gif)
Cytat
There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:
$string = $this->load->view('myfile', '', true);
Go to the top of the page
+Quote Post

2 Stron V   1 2 >
Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 3.10.2025 - 01:30