I have a table field that stores the price range. (Out of 5)
I want to display it in this manner.
If the range is 3, then display $$$ and other 2 ($) muted.
What is the best approach or what is this called?
(Check the below image for reference)
You could make use of for loop as follows:
$range = 3;
for($i=0; $i<5; $i++){
if($i < $range){
echo "<strong>$</strong>";
}else{
echo "<span class='text-muted'>$</span>";
}
}
and you have to add CSS like:
.text-muted {
color: #777;
}
Update
You can also use it as a function:
<?php
function displayRangeAsDollar($range){
$boldText = '';
$mutedText = '';
for($i=0; $i<5; $i++){
if($i < $range){
$boldText .= '$';
}else{
$mutedText .= '$';
}
}
return "<strong>".$boldText."</strong>"."<span class='text-muted'>".$mutedText."</span>";
}
echo displayRangeAsDollar(3);
Related
I've got this for loop here that stops the loop after the first loop due to the value of $due = 0. If value of $due is anything other than 0, the loop runs fine and everything works--problem is, this condition must always be zero. Bangin' my head against the wall on this one. Any ideas? Here's the loop:
for ($i = 0; $i < $unpaid->getInvoiceQuantity($customer_id); $i++) {
$each_invoice_id = $request->input('invoice_id' . $i);
$whats_due = DB::table('invoices')
->select('due')
->where('id','=', $request->input('invoice_id' . $i))
->first();
$invoice_payment_total = $request->input('total' . $i);
$inv_id_array = array('invoice_id' => $request->input('invoice_id' . $i), 'payment_id' => $payment_id,
'no_invoice' => false);
DB::table('payment_applications')->insert($inv_id_array);
$due = 0;
if ($whats_due->due == $invoice_payment_total) {
$due = 0;
}
if ($whats_due->due > $invoice_payment_total) {
$due = $whats_due->due - $invoice_payment_total;
}
$update_array = array('due' => $due);
DB::table('invoices')->where('id', $request->input('invoice_id' . $i))
->update($update_array);
$payment_application_update_array = array('amount' => $request->input('total' . $i));
DB::table('payment_applications')->where('invoice_id', $request->input('invoice_id' . $i))
->update($payment_application_update_array);
}
One of the updates must be changing the invoice quantity, and making it lower than $i, so the loop condition is no longer met.
You should save the value in a variable rather than calling the method every time.
$quantity = $unpaid->getInvoiceQuantity($customer_id);
for ($i = 0; $i < $quantity; $i++) {
// rest of loop
}
I have a database (id varchar and 4). I need copy/paste in the form text with a link („CEEOL nuo 2004 Central & Eastern European Academic Source nuo 2007 (EBSCO sąrašas)“). Then text will need to view or edit. The text should look like the original (with aktivia link).
script - index.php for view data. It's work, but from database this must be links with text.
try {
$result = $link->query('SHOW TABLES');
if (!$result) throw new MySQL_Exception($link->error);
$counter = 1;
while ($row = $result->fetch_row()) {
echo "<table><caption> {$row[1]} </caption><tr>";
$result1 = $link->query("SELECT 'Nr', 'Pavadinimas', 'Kitas pavadinimas', 'ISSN', 'Leidėjas', 'DB', 'Pastaba', ID FROM {$row[0]}");
if (!$result1) throw new MySQL_Exception($link->error);
for($i = 0; $i < $link->field_count; $i++)
{
$field_info = $result1->fetch_field();
echo "<th>{$field_info->name}</th>";
}
echo '</tr>';
$result2 = $link->query("SELECT * FROM {$row[0]} ORDER BY pavadinimas");
while ($row1 = $result2->fetch_row()) {
echo '<tr>';
echo '<td>'.$counter++.'</td>';
foreach($row1 as $_column) {
echo "<td>{$_column}</td>";
}
echo "</tr>";
}
echo '</table>';
}
}
catch (Exception $ex) {
echo 'Error MySQL: <b style="color:red;">'.$ex->getMessage().'</b>';
}
?>
My Example Tables are below,
$hdn_roll[0] =0;
$i =0;
foreach($results as $row):
$i++;
$roll = $row->roll;
$questions = $row->questions;
$hdn_roll[$i] = $roll;
$count_ad = array_count_values($hdn_roll);
$count_sp = $count_ad[$hdn_roll[$i]];
$j = 0;
if($hdn_roll[$i] != $hdn_roll[($i-1)]):
if($count_sp ==1):
create_row_after_balloting($roll,$questions);
endif;
else:
$j++;
$data['roll'.$j] = $roll;
$data['questions'.$j] = $questions;
endif;
endforeach;
Actually, I want to display Like Table:B from Table:A(MySQL Database).
Above Code, I can display 1001 to 1009 but Remain 1003,1006,1008,1006 are not display.
On the other hand, Same Roll Can't stay another(1003) after one(1003)
How can i solve it in PHP code or MySQL Query.
Try this may be its help you.
First create function in model file:
<?php
function getstudentinfo()
{
//return $this->db->get_where('studnet_info', array('status' => 3));
return $this->db->distinct('roll')->select('roll,questions')->where(array('status' => 3));
}
Then call in controller file:
function index()
{
$data['student_detail'] = $this->yourmodeelname->getstudentinfo();
$this->load->view('your view file name');
}
Then get in view file:
if(isset($student_detail)) {
foreach($student_detail as $student)
{
echo $student['roll'];
echo '</br>';
echo $student['questions'];
}
}
?>
I found the PHPExcel library brilliant to manipulate Excel files with PHP (read, write, and so on).
But nowhere in the documentation is explained how to read a XLSX worksheet to feed a MySQL table...
Sorry for this silly question, but i need it for my work and found no answer on the web.
A small example could be very helpful.
Thanks a lot.
UPDATED :
I precise my question :
The only part of code i found in the documentation that could help me is to read an Excel file and display it in a HTML table :
`require_once 'phpexcel/Classes/PHPExcel.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("edf/equipement.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
echo '<table border="1">' . "\n";
for ($row = 1; $row <= $highestRow; ++$row) {
echo '<tr>' . "\n";
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
echo '<td>' . $objWorksheet->getCellByColumnAndRow($col, $row)->getValue() . '</td>' . "\n";
}
echo '</tr>' . "\n";
}
echo '</table>' . "\n";`
I know i can use the loop to feed my MySQL table, but i don't know how... I'm not aware in OOP...
Can somebody help me, please ?
Here is the code
$inputFileName = $upload_path . $filename;
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$rows = array();
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
$rows[$col] = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
mysql_query("INSERT INTO upload (`item_number`,`qty_sold`,`cost_home`) VALUES ($rows[1],$rows[2],$rows[3])");
}
?>
I have tried mysql_query("INSERT INTO upload (col1,col2) VALUES ($rows[1],$rows[2])"); as well but didn't work. The table stays empty
The first for loops through rows, and the second one loops through columns.
So, there are plenty of solutions to your "problem".
You could, for example, populate an array and make an insert statement for each row.
As the following :
$rows = array();
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
$rows[$col] = mysql_real_espace_string($objWorksheet->getCellByColumnAndRow($col, $row)->getValue());
}
mysql_query("INSERT INTO your_table (col1,col2) VALUES ($rows[1],$rows[2])");
}
Obviously, this code can be improved.
I basically need to have take some videos information out of a database with a while loop and put them into a div. The only issue is that I need to put only 6 at a time in between a and tag and have it go to the next 6 and so forth. Here's my code:
$count = 0;
$sql = "SELECT * FROM videos ORDER BY id DESC";
$result_set = $database->query($sql);
while($videos = $database->fetch_array($result_set)) {
$count++;
// i know this is horribly wrong...
if($count == 0 || (($count % 6)+1 == 1)) {
echo '<div>';
}
// i need 6 videos to go in between the <div> and </div> tags then go on to another 6
echo "{$videos['title']}";
if($count == 0 || (($count % 6)+1 == 1)) {
echo '<div>';
}
}
This is an efficent way to do what you want:
$resultPerPage = 6;
$count = 0;
$sql = "SELECT * FROM videos ORDER BY id DESC";
$result_set = $database->query($sql);
$noPage = 1;
echo '<div id="page_1" class="pages">';
while($videos = $database->fetch_array($result_set)) {
$count++;
echo "{$videos['title']}";
if($count == $resultPerPage) {
echo '</div><div id="page_' . $noPage++ . '" class="pages">';
$count=0;
}
}
echo '</div>';