fetching data from database - php codeigniter - php

I want to fetch some data (student details) from database but my code is not working.
This is my Controller
public function Student_Detail()
{
$student_id = $this->uri->segment(3);
$record = $this->WebAdmin_Model->Student_Details($student_id);
$this->load->view('admin/template/header.php');
$this->load->view('admin/students/student_details', $record);
$this->load->view('admin/template/footer.php');
}
This is my Model
public function Student_Details($student_id)
{
$query = $this->db->query("SELECT s.`student_id`,
s.`std_email`,
s.`std_fname`,
s.`std_lname`
FROM `student_main` AS s
WHERE s.`student_id` = $student_id");
return $query->result();
}
This is my View
<section class="panel">
<div class="user-heading">
<img src="<?=base_url();?>images/profile-avatar.jpg" alt="">
</div>
<ul class="nav nav-pills nav-stacked">
<li> <?php echo $record->std_fname; ?></li>
<li> <?php echo $record->std_lname; ?></li>
<li> <?php echo $record->std_email; ?></li>
</ul>
</section>
Note that there is not problem with the query. I want to know how to fetch student details. It gives me the following error. Message : Undefined variable: record

Try this:
In your controller:
$data['record'] = $this->WebAdmin_Model->Student_Details($student_id);
$this->load->view('admin/students/student_details', $data);
And in your view:
<ul class="nav nav-pills nav-stacked">
<?php
foreach($record->$row){
?>
<li> <?php echo $row->std_fname; ?></li>
<li> <?php echo $row->std_lname; ?></li>
<li> <?php echo $row->std_email; ?></li>
<?php
}
?>
</ul>

You need to pass your model data into record array then pass into view
Controller
$student_id = $this->uri->segment(3);
$record['data'] = $this->WebAdmin_Model->Student_Details($student_id);// store data into record array
$this->load->view('admin/template/header.php');
$this->load->view('admin/students/student_details', $record);
$this->load->view('admin/template/footer.php');
Use foreach loop to retrieve data
Views
<?php
foreach($data as $record)// use foreach loop
{ ?>
<li> <?php echo $record->std_fname; ?></li>
<li> <?php echo $record->std_lname; ?></li>
<li> <?php echo $record->std_email; ?></li>
<?php } ?>

You need to do it like this, because codeigniter try to extract variable from $record and it fails. So to make possible, make it,
$record['record'] = $this->WebAdmin_Model->Student_Details($student_id);
and then in your view,
<ul class="nav nav-pills nav-stacked">
<li> <?php echo $record->std_fname; ?></li>
<li> <?php echo $record->std_lname; ?></li>
<li> <?php echo $record->std_email; ?></li>
</ul>
Because codeigniter will extract variable from $record array

Related

How I can make drop down menu from my categories mysql table only show if parent_id is not 0

So hey there as the title said I am looking for away to make my categories with subcategories. I been looking in stackoverflow for what I need but none has help me of the examples..
Here is how my table look like
So I know what I want and what I need but I have no idea how I can do that possible
I have to SELECT * FROM categories ORDER by position ASC
I have to check if parent_id is bigger then 0.
I have to remove the parent_id from my navbar and show them only under the category name where it should be by dropdown menu .
But I have no idea how I could do all of that ..
Here is how I am selecting only my categories and display them
$catsq = mysqli_query($con,"SELECT * FROM categories ORDER by position ASC");
while($catinfo=mysqli_fetch_assoc($catsq)) {
echo '
<li class="nav-item'.(isset($_GET["cat"]) && $_GET["cat"]==$catinfo["id"] ? " active" : "").'">
<a class="nav-link" href="./index.php?cat='.$catinfo["id"].'">'.$catinfo["name"].'</a>
</li>
';
}
and it's look like this
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="cat=1">TestCat</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cat=2">TestCat2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cat=3">TestSub</a>
</li>
</ul>
but I want It to look like this
<ul class="nav navbar-nav">
<li class="">TestCat</li>
<li class="dropdown ">
//TestCat2 have to doing nothing always.
TestCat2</i>
<ul class="dropdown-menu">
<li><a class="nav-link" href="cat=3">TestSub</a></li>
</ul>
</li>
</ul>
when the parent_id is more then 0..
If anyone can help me with this would be great..
Thanks to everybody.
There are several approaches you can take:
Build an array
Nested queries
Recursion
Array
This approach builds a data structure that you can iterate through in your view. Working example
<?php
// get db connection...
// create categories array
$stmt = mysqli_query($con, "SELECT * FROM categories ORDER BY position ASC");
while( $row = mysqli_fetch_assoc($stmt)) {
// $category[ $row['parent_id] ][ $row['id'] ] = $row; // use if you need to access other fields in addition to name
$category[ $row['parent_id] ][ $row['id'] ] = $row['name'];
}
// other php stuff...
?>
<html>
... snip ...
<ul class="nav navbar-nav">
<?php foreach($category[0] as $id => $name): ?>
<?php if( isset( $category[$id]) ): ?>
<li class="dropdown ">
<?= $name ?>
<ul class="dropdown-menu">
<?php foreach($category[$id] as $sub_id => $sub_name): ?>
<li><a class="nav-link" href="?cat=<?= $sub_id ?>" ><?= $sub_name ?></a></li>
<?php endforeach; ?>
</ul>
</li>
<?php else: ?>
<li class="">
<?= $name ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Nested Queries
This method is easiest to display using an imaginary class that does all the sql stuff behind the scenes. For the sake of argument, we will assume a class Category that has a method named listByParent($parent_id) which returns a list of rows having the designated parent_id.
<?php
$cat = new Category();
$topLevel = $cat->listByParent(0);
?>
<html>
... snip ...
<ul class="nav navbar-nav">
<?php foreach( $topLevel as $topRow ): ?>
<!-- note, this method is run on every iteration of top level categories -->
<?php $subRows = $cat->listByParent($topRow['id']) ?>
<?php if( count($subRows)): ?>
<li class="dropdown ">
<?= $topRow['name'] ?>
<ul class="dropdown-menu">
<?php foreach($subRows as $row): ?>
<li><a class="nav-link" href="?cat=<?= $row['id'] ?>" ><?= $row['name'] ?></a></li>
<?php endforeach; ?>
</ul>
</li>
<?php else: ?>
<li class="">
<?= $topRow['name'] ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Recursion
Using recursion would allow you to have “unlimited” levels of subcategories. However, it’s a level of complexity that does not seem warranted in this case. But should you want to pursue it, note that the best way to approach it would be to make a template for the html that could be accessed programatically, with $cat->findByParent() being a key player...

how to get the HREF value using POST

I'm making a dynamic menu where each link in my dropdown menu will be directed to a page base on the ID in the HREF link. How can I POST the ID from my link?
Here's my code:
<?php
$lesson_sql = "SELECT * FROM lesson WHERE lessonID = 1";
$lesson_query = mysqli_query($db, $lesson_sql);
$lesson = mysqli_fetch_assoc($lesson_query);
?>
<nav id="navbar">
<ul id="navmenu">
<div class="navmenu">
<li><strong>Catalog</strong><span class="darrow"></span>
<ul class="sub1">
<li>Topic 1
<ul class="sub1_1">
<?php
do {
?>
<li>
<a href="lesson1.1.php?lessonID=<?php echo $lesson['lessonID'];?>">
<?php echo $lesson['lessonName']; ?>
</a>
</li>
<?php
}while ($lesson = mysqli_fetch_assoc($lesson_query));
?>
</ul>
</li>
</ul>
</li>
</div>
</ul>
</nav>
And here is the code from the code above I wanted to get the ID, its beside the href value.
<li><?php echo $lesson['lessonName']; ?></li>
Sorry for my english, I hope you understand me!
in your lesson1.1.php file you can use $_GET to access the id as below.
$id = $_GET["lessonID"];
If you have query string then you use
$_GET['name of control']

Issue in pulling pages from the database

I am having problems pulling the pages through in PHP and HTML I have used :-
<li>
<!-- Pulling Categories from the database
dynamically -->
<?php
$nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject =
mysqli_fetch_assoc($nav_subjects)) {
?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
Which pulls the categories dynamically from the database and displays them with a drop down arrow just how I wanted but the pages will not show underneath them here's the code I have used for that bit:-
<!-- Categories listed correctly let's pull the pages for each one -->
<?php
if($nav_subject['id'] == $subject_id) {
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) {
?>
<ul>
<li>
<?php echo h($nav_page['menu_name']); ?>
</li>
</ul>
<?php } // while $nav_pages
} // if($nav_subject['id'] == $subject_id)
} // while $nav_subjects ?>
</li>
<?php
mysqli_free_result($nav_subjects);
mysqli_free_result($nav_pages);
?>
I am pulling in the SQL from another page which is loaded correctly as the categories load and display correctly.
I will be grateful for any ideas.
I have also tried to echo back the sql result but nothing is shown.
I have now got it working with the following code:-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject = mysqli_fetch_assoc($nav_subjects)) {?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<li><?php echo h($nav_page['menu_name']); ?></li>
<?php } ?>
<?php } ?>
</ul>
But now it is listing the secondary subjects as a child of the first instead of individual subjects of their own.
*********Resolved**********
please advise if you think the code could be better i've currently used :-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);?>
<?php while($nav_subject = mysqli_fetch_assoc($nav_subjects)){?>
<span class="opener"><?php echo h($nav_subject['menu_name']);?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<a href="<?php echo url_for('index.php?id=' . h(u($nav_page['id']))); ?>">
<?php echo h($nav_page['menu_name']); ?></a>
<?php } ?>
<?php mysqli_free_result($nav_pages); ?>
</ul>
<?php } ?>
<?php mysqli_free_result($nav_subjects); ?>
</li>

How to call user specific data from an external database into WordPress

Here is the challenge and I am sure this is not the first time someone has came into this issue however have not found a viable solution pertaining to this specific process within the pages of stackoverflow or any other development forum for that matter.
Here is the task I am trying to complete.
I have built a WordPress website which will be a members area where only registered users may log into the site to view its content. This functionality will be done through WordPress however I have a page within WordPress titled "Member Status" that I have created a custom page template for in order to pull information that will be user specific to a form on that page. The user specific information is being pulled from a secondary external database to the WP database.
Here is the code for the custom page template for a better visual of what I am trying to do. Basically all form fields would be populated from the specific row of that specified user. however the query seems to fail everytime and am wondering if I should be adding this table to my WP db, if that would rectify the issue or is there a fault in my code I am just not seeing.
<?php
$servername = "localhost";
$username = "DBusername";
$password = "DBpassword";
$database = "DBname";
// Create connection
$conn = new mysqli($servername, $username, $password);
$db_selected = mysqli_select_db($conn, $database);
$userid = $current_user->user_login;
$sql = "SELECT * FROM 'member-status' WHERE 'Card' = '$userid'";
$result = mysqli_query($conn, $sql);
if ($result) {
list($userid) = mysqli_fetch_array(mysqli_query($result));
} else {
echo "query failed";
}
if (mysqli_connect_errno($conn))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
while ($row = mysqli_fetch_assoc("$sql"))
?>
<div class="Disclaimer_webpage">
<?php echo $row["Disclaimer_WebPage"]; ?>
</div>
<div class="OuterBlock">
<div class="oneblock">
<ul>
<li>LAST</li>
<li><?php echo $row["Last"]; ?></li>
</ul>
<ul>
<li>FIRST</li>
<li><?php echo $row["First"]; ?></li>
</ul>
<ul>
<li>CARD</li>
<li><?php echo $row["Card"]; ?></li>
</ul>
<ul>
<li>CLASS</li>
<li><?php echo $row["Class"]; ?></li>
</ul>
</div>
<div class="secblock">
<ul>
<li>DATE SIGNED BOOK</li>
<li>
<?php if ($row['StartDate']!=""){ echo date('m/d/y', strtotime($row["StartDate"])); } ?>
</li>
</ul>
<ul>
<li>OUT OF WORK DATE</li>
<li>
<?php if($row['CURROOWD']!=""){ echo date('m/d/y', strtotime($row["CURROOWD"])); } ?>
</li>
</ul>
<ul>
<li>DISPATCH UNIT</li>
<li><?php echo $row["OriginalUnit"]; ?></li>
</ul>
<ul>
<li>DAYS DISPATCHED (OF 140)</li>
<li><?php echo $row["DAYS140"]; ?></li>
</ul>
</div>
<div class="over_all_list_position">
<ul>
<li><b>OVER ALL LIST POSITION</b><span class="information_Icon"><img src="http://redsealcreative.com/clients/353-members/wp-content/plugins/IBEWJOBLISTS/Info.png" > <p class="imgDescription">Your position on the Out of Work List.</p></span><span class="see_below"> (*see below)</span></li>
<li><?php echo $row["ListPosition"]; ?></li>
</ul>
<ul>
<li><b>UNIT LIST POSITION</b><span class="information_Icon1"><img src="http://redsealcreative.com/clients/353-members/wp-content/plugins/IBEWJOBLISTS/Info.png" > <p class="imgDescription1">Your list position relative only to members in your Dispatch Unit.</p><span></li>
<li><?php echo $row["LISTPOS_UNIT"]; ?></li>
</ul>
<ul>
<li><b>OPG LIST POSITION</b><span class="information_Icon2"><img src="http://redsealcreative.com/clients/353-members/wp-content/plugins/IBEWJOBLISTS/Info.png" > <p class="imgDescription2">Your list position relative only to those with current OPG Security Clearance.</p><span></li>
<li><?php echo $row["LISTPOS_OPG"]; ?></li>
</ul>
</div>
<div class="fothblock">
<ul>
<li>PASSES</li>
<li>NORTH UNIT</li>
<li>SOUTH UNIT</li>
<li>EAST UNIT</li>
</ul>
<ul>
<li>ACCUMULATED</li>
<li><?php echo $row["PassNorthAcc"]; ?></li>
<li><?php echo $row["PassSouthAcc"]; ?></li>
<li><?php echo $row["PassEastAcc"]; ?></li>
</ul>
<ul>
<li>MAXIMUM</li>
<li><?php echo $row["PassNorthMax"]; ?></li>
<li><?php echo $row["PassSouthMax"]; ?></li>
<li><?php echo $row["PassEastMax"]; ?></li>
</ul>
</div>
<div class="sixblock">
<ul>
<li>LAST OUT WORK DATES DISPATCHED</li>
<li>NORTH</li>
<li>SOUTH</li>
<li>EAST</li>
<li>OUT OF TOWN</li>
</ul>
<ul>
<li class="padding-top"></li>
<li class="padding-top"><?php if($row['LOOWDN']!=""){echo date('m/d/y', strtotime($row["LOOWDN"])); }?></li>
<li><?php if($row['LOOWDS']!=""){ echo date('m/d/y', strtotime($row["LOOWDS"])); } ?></li>
<li class="padding-top"><?php if($row['LOOWDE']!=""){echo date('m/d/y', strtotime($row["LOOWDE"]));} ?></li>
<li><?php if($row['OOTOOWD']!=""){echo date('m/d/y', strtotime($row["OOTOOWD"]));} ?></li>
</ul>
</div>
<div class="seven">
<ul>
<li>LAST UPDATED</li>
</ul>
<ul>
<li class="padding-top"><?php echo $row["TIME_STAMP"]; ?></li>
</ul>
</div>
</div>
<div class="Disclaimer_POL">
<?php echo $row["Disclaimer_POL"]; ?>
</div>
All insight will be gratefully appreciated. Thanks in advance for the communities assistance to a new member. :)
UPDATE #2 ISSUE RESOLVED
I am happy to announce that I figured out the issue. For anyone else that is wanting to accomplish this task here is the working code that I used.
<?php global $current_user;
wp_get_current_user(); ?>
<?php
// $mydb = new wpdb('username','password','database','localhost');
//$mydb->show_errors();
$userid = $current_user->user_login;
$result = $wpdb->get_results( "SELECT * FROM member_status WHERE CARD = $userid");
// $query = "SELECT * FROM member_status WHERE CARD = $userid";
// $result = $mydb->get_results($query);
?>
<?php foreach ( $result as $query ) {?>
Then to call the individual fields/column data add this code where you want it located
<?php echo $query->ColumnName; ?> //Change ColumnName to the column you want to call from.
I hope this comes in handy for you all :)
I am happy to announce that I figured out the issue. For anyone else that is wanting to accomplish this task here is the working code that I used.
<?php global $current_user;
wp_get_current_user(); ?>
<?php
// $mydb = new wpdb('username','password','database','localhost');
//$mydb->show_errors();
$userid = $current_user->user_login;
$result = $wpdb->get_results( "SELECT * FROM custom_table WHERE Column = $userid");
// $query = "SELECT * FROM custom_table WHERE Column = $userid";
// $result = $mydb->get_results($query);
?>
<?php foreach ( $result as $query ) {?>
Then to call the individual fields/column data add this code where you want it located
<?php echo $query->ColumnName; ?> //Change ColumnName to the column you want to call from.
I hope this comes in handy for you all :)

how to generate a serverside menu with 3 levels?

I am trying to generate the bellow menu dynamically using php and mysql
<ul id="prod_nav" class="clearfix">
<!-- top --> <li class="active"><span class="down">Clothes</span>
<ul>
<h1> Men </h1>
<li>Shirt </li>
<li>T-shirt </li>
<li>Polo shirt </li>
<li>Formal shoes </li>
<li>Sport shoes </li>
<li>Suit </li>
<li>Underwear </li>
<li>Socks </li>
<li>Pants </li>
</ul>
<ul>
<h1> Women </h1>
<li>Shirt </li>
<li>T-shirt </li>
<li>Polo shirt </li>
<li>High heel shoes </li>
<li>Sport shoes </li>
<li>Wedding clothes </li>
<li>Underwear </li>
<li>Leather </li>
<li>Socks </li>
<li>Pants </li>
</ul>
</li>
</ul>
but I am not sure which way is the best principals for generating the menu?
should I use while loop and if or Case or for loop?
which way is the best way?
Thanks
You should have an array
$a=array('shirt','t-shirt','polo shirt','formal shoes','sport shoes','suit','underwear','socks','pants');
and use it like this:
<ul>
<h1>MEN</h1>
<?php foreach($a as $val) :?>
<li><?php echo $val; ?></li>
<?php endforeach ;?>
</ul>
<ul>
<h1>womaen</h1>
<?php foreach($a as $val) :?>
<li><?php echo $val; ?></li>
<?php endforeach ;?>
</ul>
$gender = array('men','women');
$productlist = array('shirt','t-shirt','polo shirt','formal shoes','sport shoes','suit','underwear','socks','pants');
foreach($gender as $individual) {
echo "<h2>{$individual}</h2>";
echo "<ul>";
foreach($productlist as $product) {
<li><?php echo $product; ?></li>
}
echo "</ul>";
}
can you please used this:
$gender = array('men','women');
$productlist = array('shirt'=>'shirt_url.php','t-shirt'=>'t-shirt_url.php','polo shirt'=>'polo_shirt.php');
foreach($gender as $individual) {
echo "<h2>{$individual}</h2>";
echo "<ul>";
foreach($productlist as $product_key=>$product_url) {
?>
<li><?php echo $product_key; ?></li>
<?php
}
echo "</ul>";
}

Categories