Random data listing and grouping - php

I have a 9-person classroom. I want to randomly divide the students in this class into three-person classes. It keeps returning the same names in my query. How can I do this, can you help?
I listed all the students first. Then I want to randomly group them into three.
My source code:
<?php
require 'db.php';
$students = $db->query('SELECT * FROM students')->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="container mt-2">
<div>
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">No</th>
</tr>
</thead>
<tbody>
<?php foreach($students as $student): ?>
<tr>
<th scope="row"><?php echo $student['id'] ?></th>
<td><?php echo $student['name'] ?></td>
<td><?php echo $student['no'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
$randomstudents = $db->query('select * from students order by rand() limit 3')->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="row">
<div class="col-md-3">
<h6>3 Person Classroom</h6>
<hr>
<ul>
<?php foreach($randomstudents as $rs): ?>
<li><?php echo $rs['name']; ?></li>
<?php endforeach; ?>
</ul>
</div>
<div class="col-md-3 offset-md-1">
<h6>3 Person Classroom</h6>
<hr>
<?php foreach($randomstudents as $rs): ?>
<li><?php echo $rs['name']; ?></li>
<?php endforeach; ?>
</div>
<div class="col-md-3 offset-md-1">
<h6>3 Person Classroom</h6>
<hr>
<?php foreach($randomstudents as $rs): ?>
<li><?php echo $rs['name']; ?></li>
<?php endforeach; ?>
</div>
</div>
</div>

you can try to use array_chunk()
<?php
$randomstudents = $db->query('select * from students order by rand()')
->fetchAll(PDO::FETCH_ASSOC);
$randomstudents = array_chunk($randomstudents, 3);
foreach ($randomstudents as $groupOfThree) :
?>
<div class="col-md-3">
<h6>3 Person Classroom</h6>
<hr>
<ul>
<?php foreach($groupOfThree as $rs): ?>
<li><?php echo $rs['name']; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>

Related

sql make column name not duplicate and show all other value in same table

I don't want duplicate column name of company_name and when I click collapse bootstrap it shows the table values that has the same name of company_name
this is output now
<?php
$stmt=$conn->prepare("SELECT DISTINCT company_name,project,po_number FROM company");
$stmt->execute();
$result=$stmt->get_result();
?>
<div class="accordion" id="accordionExample">
<?php while($row = mysqli_fetch_array($result)){?>
<div class="card">
<div class="card-header" >
<h2 class="mb-0">
<a class="btn cat" type="button" data-toggle="collapse" href="#c<?=$row['company_name']?>" role="button" aria-expanded="false">
<?php echo $row['company_name'] ?>
</a>
</h2>
</div>
<div id="c<?=$row['company_name']?>" class="collapse">
<div class="card-body">
<div class="table-responsive ">
<table class="table table-hover table-borderless" id="table1">
<thead>
<tr class="text-truncate">
<th>Po</th>
<th>ชื่อโครงการ</th>
<th>ผู้รับผิดชอบ</th>
<th>รายละเอียด</th>
</tr>
</thead>
<tbody>
<?php $c; ?>
<tr class="<?=($c++%2==1) ? 'odd' : 'even' ?>">
<td><?php echo $row['po_number'] ?></td>
<td><?php echo $row['project'] ?></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
I want my collapse show like this hope you understand
https://i.stack.imgur.com/ZtwEe.png
Perhaps it will be cleanest to create a grouped array structure from your result set, then use nested loops to generate the output from the new structure.
Suggested Code:
<?php
$sql = "SELECT DISTINCT company_name, project, po_number FROM company";
$grouped = [];
foreach ($conn->query($sql) as $row) {
$grouped[$row['company_name']][] = [
'project' => $row['project'],
'po_number' => $row['po_number']
];
}
// now generate markup...
?>
<div class="accordion" id="accordionExample">
<?php foreach ($grouped as $companyName => $details) { ?>
<div class="card">
<div class="card-header">
<h2 class="mb-0">
<a class="btn cat" type="button" data-toggle="collapse"
href="#c<?=$companyName?>" role="button" aria-expanded="false"
>
<?=$companyName?>
</a>
</h2>
</div>
<div id="c<?=$companyName?>" class="collapse">
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover table-borderless">
<thead>
<tr class="text-truncate">
<th>Po</th>
<th>ชื่อโครงการ</th>
<th>ผู้รับผิดชอบ</th>
<th>รายละเอียด</th>
</tr>
</thead>
<tbody>
<?php foreach ($details as $i => $detail) { ?>
<tr class="<?=$i & 1?'odd':'even'=>">
<td><?=$detail['po_number']?></td>
<td><?=$detail['project']?></td>
<td></td>
<td></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php } ?>
</div>

How to Hide or Show an included with PHP

I'm trying to hide an included file with a condition.
<?php include('header_dashboard.php'); ?>
<?php include('session.php'); ?>
<?php $get_id = $_GET['id']; ?>
<?php
$post_id = $_GET['post_id'];
if($post_id == ''){
?>
<script>
window.location = "assignment_student.php<?php echo '?id='.$get_id; ?>";
</script>
<?php
}
?>
<body id="studentTableDiv">
<?php include('navbar_student.php'); ?>
<div class="container-fluid">
<div class="row-fluid">
<?php include('assignment_link_student.php'); ?>
<div class="span6" id="content">
<div class="row-fluid">
<!-- breadcrumb -->
<?php $class_query = mysqli_query($conn, "select * from teacher_class
LEFT JOIN class ON class.class_id = teacher_class.class_id
LEFT JOIN subject ON subject.subject_id = teacher_class.subject_id
where teacher_class_id = '$get_id'")or die(mysqli_error());
$class_row = mysqli_fetch_array($class_query);
?>
<ul class="breadcrumb">
<li><?php echo $class_row['class_name']; ?> <span class="divider">/</span></li>
<li>Asignaciones <span class="divider">/</span></li>
<li><b>Subir Asignación</b></li>
</ul>
<!-- end breadcrumb -->
<!-- block -->
<div id="block_bg" class="block">
<div class="navbar navbar-inner block-header">
<div id="" class="muted pull-left"></div>
</div>
<div class="block-content collapse in">
<div class="span12">
<?php
$query1 = mysqli_query($conn, "select * FROM assignment where assignment_id = '$post_id'")or die(mysqli_error());
$row1 = mysqli_fetch_array($query1);
?>
<div class="alert alert-info">Subir respuesta de asignacion en : <?php echo $row1['fname']; ?></div>
<div id="">
<table cellpadding="0" cellspacing="0" border="0" class="table" id="">
<thead>
<tr>
<th>Fecha Subida</th>
<th>Nombre de Archivo</th>
<th>Descripción</th>
<th>Enviado por:</th>
<th>Calificación</th>
</tr>
</thead>
<tbody>
<?php
$query = mysqli_query($conn, "select * FROM student_assignment LEFT JOIN student on student.student_id = student_assignment.student_id where assignment_id = '$post_id' order by assignment_fdatein DESC")or die(mysqli_error());
while($row = mysqli_fetch_array($query)){
$id = $row['student_assignment_id'];
$student_id = $row['student_id'];
?>
<tr>
<td><?php echo $row['assignment_fdatein']; ?></td>
<td><?php echo $row['fname']; ?></td>
<td><?php echo $row['fdesc']; ?></td>
<td><?php echo $row['firstname']." ".$row['lastname']; ?></td>
<?php if ($session_id == $student_id){ ?>
<td>
<span class="badge badge-success"><?php echo $row['grade']; ?></span>
</td>
<?php }else{ ?>
<td></td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- /block -->
</div>
</div>
<?php include('submit_assignment_sidebar.php') ?>
</div>
<?php include('footer.php'); ?>
</div>
<?php include('script.php'); ?>
</body>
</html>
<?php include('submit_assignment_sidebar.php') ?>
OK what I'm trying to do is move this include that is at the end
<?php include('submit_assignment_sidebar.php') ?>
To:
<?php if ($session_id == $student_id){ ?>
<td>
<span class="badge badge-success"><?php echo $row['grade']; ?></span>
</td>
<?php }else{ ?>
<td></td>
<?php include('submit_assignment_sidebar.php') ?>
<?php } ?>
But it doesn't work. Doesn't include de sidebar file. Any sugestion?

undefined variable sub_total. even after defining the variable

<?php
include('includes/db.php');
?>
<?php include('functions/functions.php'); ?>
<?php
include('header.php');
?>
<?php
include('topheader.php');
?>
<?php
include('nav.php');
?>
<div id="content">
<div class="container">
<div class="col-md-12">
<ul class="breadcrumb">
<li>Home</li>
<li>Cart</li>
</ul> <!--breadcrumb ends--->
</div> <!---col-md-12 ends-->
<div class="col-md-9" id="cart">
<div class="box">
<form action="cart.php" method="post" enctype="multipart-form-data">
<h1>Shopping Cart</h1>
<?php
$ip_add = getUserIP();
$select_cart = "select * from cart where ip_add='$ip_add'";
$run_cart = mysqli_query($conn, $select_cart);
$count = mysqli_num_rows($run_cart);
?>
<p class="text-muted">You currently have <?php echo $count; ?> items in your cart</p>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th colspan="2">Product</th>
<th>Quantity</th>
<th colspan="1">Unit Price</th>
<th>Container</th>
<th colspan="1">Delete</th>
<th colspan="2">Sub Total</th>
</tr>
</thead> <!--thead ends-->
<tbody>
<?php
$total = 0;
while ($row_cart = mysqli_fetch_array($run_cart)) {
$pro_id = $row_cart['p_id'];
$pro_size = $row_cart['size'];
$pro_qty = $row_cart['qty'];
$get_products = "select * from products where product_id='$pro_id'";
$run_products = mysqli_query($conn, $get_products);
while ($row_products = mysqli_fetch_array($run_products)) {
$product_title = $row_products['product_title'];
$product_img1 = $row_products['product_img1'];
$only_price = $row_products['product_price'];
$sub_total = $row_products['product_price'] * $pro_qty;
$total += $sub_total;
}
?>
<tr>
<td><img src="admin_area/product_images/<?php echo $product_img1; ?>"></td>
<td><?php echo $product_title; ?></td>
<td><?php echo $pro_qty; ?></td>
<td>₹<?php echo $only_price; ?>.00</td>
<td><?php echo $pro_size ?></td>
<td><input type="checkbox" name="remove[]" value="<?php echo $pro_id; ?>"></td>
<td>₹<?php echo $sub_total; ?>.00</td>
</tr> <!---tr ends-->
<?php } ?>
</tbody>
<tfoot>
<tr>
<th colspan="5">TOTAL</th>
<th colspan="2">₹ <?php echo $total; ?>.00</th>
</tr>
</tfoot> <!--tfoot ends-->
</table> <!--table ends-->
</div> <!---table-responsive end-->
<div class="box-footer">
<div class="pull-left">
<a href="index.php" class="btn btn-default">
<i class="fa fa-chevron-left"></i>Continue Shopping
</a>
</div> <!--pullleft ends-->
<div class="pull-right">
<button class="btn btn-default" type="submit" name="update" value="Update Cart"><i class="fa fa-refresh"></i>Update Cart </button>
<a href="checkout.php" class="btn btn-primary">
Proceed to checkout<i class="fa fa-chevron-right"></i>
</a>
</div> <!--pullright ends-->
</div> <!----box footer ends-->
</form> <!---form ends-->
</div> <!---box ends-->
<?php
function update_cart() {
global $conn;
if (isset($_POST['update'])) {
foreach ($_POST['remove'] as $remove_id) {
$delete_product = "delete from cart where p_id='$remove_id'";
$run_delete = mysqli_query($conn, $delete_product);
if ($run_delete) {
echo "<script>window.open('cart.php','_self')</script>";
}
}
}
}
echo #$up_cart = update_cart();
?>
<div class="row same-height-row">
<div class="col-md-3 col-md-6">
<div class="box same-height headline">
<h3 class="text-center">Recently Viewed Product</h3>
</div> <!----box same-height headline end-->
</div> <!---col-m-3 col-md-6 ends---->
<?php
$get_products = "select * from products order by rand() LIMIT 0,3";
$run_products = mysqli_query($conn, $get_products);
while ($row_products = mysqli_fetch_assoc($run_products)) {
$pro_id = $row_products['product_id'];
$pro_title = $row_products['product_title'];
$pro_price = $row_products['product_price'];
$pro_img1 = $row_products['product_img1'];
echo "<div class='center-responsive col-md-3 col-sm-6'>
<div class='product same-height'>
<a href='details.php?pro_id=$pro_id'>
<img src='admin_area/product_images/$pro_img1' class='img-responsive'>
</a>
<div class='text'>
<h3><a href='details.php?pro_id=$pro_id'>$pro_title</a></h3>
<p class='price'>₹ $pro_price</p>
</div>
</div>
</div>";
}
?>
</div>
</div>
<div class="col-md-3">
<div class="box" id="order-summary">
<div class="box-header">
<h3>Order Summary</h3>
</div> <!--box-header ends-->
<p class="text-muted">
Shipping and additional costs are calulated based on the value you have entered.
</p>
<div class="table-responsive">
<table class="table">
<tbody>
<tr>
<td>Order Subtotal</td>
<th>₹<?php echo $sub_total; ?></th>
</tr>
<tr>
<td>Shipping and Handling</td>
<td>₹0.00</td>
</tr>
<tr>
<td>Tax</td>
<td>₹0.00</td>
</tr>
<tr class="total">
<td>Total</td>
<th>₹ <?php echo $total; ?></th>
</tr>
</tbody>
</table> <!--table ends-->
</div> <!---table responsive ends-->
</div> <!---box ends-->
</div> <!----col-md-3 ends--->
</div> <!--container ends-->
</div> <!---content ends-->
<?php include_once 'footer.php'; ?>
i get undefined variable sub_total even after defining variable subtotal above. Can someone figure out what the problem is?? I have check all curly brackets and semicolen. Due on a project that needs to be submited tomorrow. I am thinking improving the coding later but the basic functionality is not working.

Display rating above Reviews

We are using following code for displaying review & ratings section. I want to display stars above "Title": This is really very good product
<?php $_items = $this->getReviewsCollection()->getItems();?>
<div class="box-collateral box-reviews" id="customer-reviews">
<?php if (count($_items)):?>
<div class="review-heading">
<h2>
<?php echo $this->__('') ?>
<span class="reviewtab">
<?php
// echo count($_items);
?>
Ratings & Reviews
</span>
</h2>
<?php echo $this->getChildHtml('toolbar') ?>
</div>
<dl>
<?php foreach ($_items as $_review):?>
<dt>
<a href="<?php echo $this->getReviewUrl($_review->getId()) ?>">
<?php echo $this->escapeHtml($_review->getTitle()) ?>
</a>
</dt>
<dd>
<?php $_votes = $_review->getRatingVotes(); ?>
<?php echo nl2br($this->escapeHtml($_review->getDetail())) ?>
<?php if (count($_votes)): ?>
<table class="ratings-table">
<colgroup>
<col class="review-label" />
<col class="review-value" />
</colgroup>
<tbody>
<?php foreach ($_votes as $_vote): ?>
<tr>
<th><?php echo $this->escapeHtml($_vote->getRatingCode()) ?></th>
<td>
<div class="rating-box">
<div class="rating" style="width:<?php echo $_vote->getPercent() ?>%;"></div>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<span class="review-meta">
<?php echo $this->__('Review by %s', $this->escapeHtml($_review->getNickname())) ?>
/
<?php echo $this->__('(Posted on %s)', $this->formatDate($_review->getCreatedAt()), 'long') ?>
</span>
</dd>
<?php endforeach; ?>
</dl>
<?php // echo $this->getChildHtml('toolbar') ?>
<?php elseif($this->getParentBlock()): ?>
<?php echo $this->getParentBlock()->getReviewsSummaryHtml($this->getProduct(), 'short', true)?>
<?php endif;?>
<?php echo $this->getChildHtml('review_form') ?>
</div>
When I tried with css, it didn't work for me.
I tried swapping code, but it didn't work for me either. Am I doing a mistake by swapping?
Please help me to find solution.
Thanks in advance
This should help, you might need to make minor css changes i think.
<?php $_items = $this->getReviewsCollection()->getItems();?>
<div class="box-collateral box-reviews" id="customer-reviews">
<?php if (count($_items)):?>
<div class="review-heading">
<h2>
<?php echo $this->__('') ?>
<span class="reviewtab">
<?php
// echo count($_items);
?>
Ratings & Reviews
</span>
</h2>
<?php echo $this->getChildHtml('toolbar') ?>
</div>
<dl>
<?php foreach ($_items as $_review):?>
<dt>
<?php $_votes = $_review->getRatingVotes(); ?>
<?php if (count($_votes)): ?>
<table class="ratings-table">
<colgroup>
<col class="review-label" />
<col class="review-value" />
</colgroup>
<tbody>
<?php foreach ($_votes as $_vote): ?>
<tr>
<th><?php echo $this->escapeHtml($_vote->getRatingCode()) ?></th>
<td>
<div class="rating-box">
<div class="rating" style="width:<?php echo $_vote->getPercent() ?>%;"></div>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<a href="<?php echo $this->getReviewUrl($_review->getId()) ?>">
<?php echo $this->escapeHtml($_review->getTitle()) ?>
</a>
</dt>
<dd>
<?php echo nl2br($this->escapeHtml($_review->getDetail())) ?>
<span class="review-meta">
<?php echo $this->__('Review by %s', $this->escapeHtml($_review->getNickname())) ?>
/
<?php echo $this->__('(Posted on %s)', $this->formatDate($_review->getCreatedAt()), 'long') ?>
</span>
</dd>
<?php endforeach; ?>
</dl>
<?php // echo $this->getChildHtml('toolbar') ?>
<?php elseif($this->getParentBlock()): ?>
<?php echo $this->getParentBlock()->getReviewsSummaryHtml($this->getProduct(), 'short', true)?>
<?php endif;?>
<?php echo $this->getChildHtml('review_form') ?>

how change div class according to a php condition - CodeIgniter

I want to change the color of the div of this table (all records that status = no in the database ),Which means unread message should have a different div class. I have no idea how to do this i am using codeigniter as my framework
My view
<div class="row">
<div class="span10 box" style="padding: 10px;">
<?php echo form_open('admin/messages'); ?><!-- start of the form -->
<p>Recent Messages</p>
<table class="table hovered">
<thead>
<tr class="selected">
<th class="text-left">Name</th>
<th class="text-left">Email</th>
<th class="text-left">Phone</th>
<th class="text-left">Message</th>
</tr>
</thead>
<tbody>
<?php
if(isset($records)) :
foreach($records as $row) :
?>
<tr>
<td class="right"><?php echo $row->contactus_name; ?></td>
<td class="right"><?php echo $row->contactus_email; ?></td>
<td class="right"><?php echo $row->contactus_phone; ?></td>
<td class="right tertiary-text-secondary text-justify"><?php echo $row->contactus_comment; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot></tfoot>
</table>
<?php else : ?>
<p>No records</p>
<?php endif; ?>
</div>
<div class="span3">
<nav class="sidebar dark">
<ul>
<li>
<a href="#">
<i class="icon-home"></i>
Inbox <strong>
<?php if(isset($count)){echo '<div class="brand"><div class="badge bg-red">'.$count.'</div></div>';}?>
</strong>
</a>
</li>
<li>
<a href="#">
<i class="icon-home"></i>
Send
</a>
</li>
<li>
<a href="#">
<i class="icon-home"></i>
Newsletter/Ad
</a>
</li>
</ul>
</nav>
</div>
<?php echo form_close(); ?><!-- END of the form -->
</div>
Providing a link to / your code would allow us to give far more specific advice, but in general:
<?php if ($foo == 'bar') { $class_name = 'class1'; } else { $class_name = 'class2'; } ?>
<div class="<?php echo $class_name; ?>">
foreach($results as $row)
{
echo '<div '. ($row->read == 0 ? ' class="unread"': '') .'>Your content</div>';
}

Categories