How to sort a table by ID number automatically? - php

I just want to sort my table by ID number... how can I that with the simplest way?
I want to sort that table by "1" "2" "3" automatically.. it will start 1 to 3 (from little one to bigger numbers.)
<table>
<tr>
<th>ID numbers</th>
<th>Names</th>
</tr>
<tr>
<td>1</td>
<td>haluk</td>
</tr>
<tr>
<td>2</td>
<td>betul</td>
</tr>
<tr>
<td>3</td>
<td>Erdem</td>
</tr>
<tr>
<td>5</td>
<td>Eylül</td>
</tr>
Thanks...

To sorting the table automatically via Pure HTML seems impossible at all. You still need to use PHP to make a quick sorting. But first all the data must be inside of your database table. An to simply echo out all the info just do some simple looping
<?php
$bil = 1;
$SQL = "SELECT * FROM tablename";
$Query = mysqli_query($connection , $SQL);
while ($row = mysqli_fetch_array($Query)) {
echo "
<tr><td>".$bil++."</td></tr>
";
}
?>
If don't understand feel free to ask

<?php
include('database_connection.php');
$sorgu = $baglanti->query("select * from makale ORDER BY ID;");
while ($sonuc = $sorgu->fetch_assoc()) {
?>
I added that code in front of my table and fetch_assoc my datas to my table.. than all ordered by ID numbers...

Related

How to display static values in td column in codeigniter

Hi all i have one table there i need to display the course report so every course has one stage so that i have 16 types of stages ex: On Hold,Asset Incomplete,SME Discussion..etc..Like 16 types i have So i would like to display 16 types in mt td column
<table class="table table-bordered">
<thead>
<tr><th class="center">#</th>
<th class="center">PHY</th>
<th class="center">CHE</th>
<th class="center">ZOO</th>
<th class="center">BOT</th>
<th class="center">MATH</th>
<th class="center">Total</th>
</TR>
</thead>
<tbody>
<tr>
<td>On Hold</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>
Remaining td's i have to get count of every course and to display
Can anyone help me how can i do that.
Thanks in advance.
You can do it by taking all your static string as an associative array for example
$arr = array(0=>'On Hold',1=>'Asset Incomplete',2=>'SME Discussion');
and your td should be
<?php $i = 0;
foreach($data as $dt){ ?>
<td><?php echo $arr[$i]; ?></td>
<?php $i++; } ?>
If you want to pass a variable from your controller to your view, you do so in the $data variable.
//Controller
$this->load->model('mymodel);
$data['var1'] = "Some value";
$data['var2'] = "Some value";
$data['query'] = $this->mymodel->thefunction();
//Model = mymodel
//function = thefunction(), you have to obviously create the class and function, if you don't know how to do that, let me know
$sql = "SELECT Count(courseid) as 'statusCount'";
$query = $this->db->query($sql);
When you now want to display those variables in your view template, call the variable by it's name, without the "$data part"
//In your view
<?php
foreach($query->result() as $row) {
echo "<td>" . $row->statusCount . "</td>";
}
?>
Please set the array with two fields.
One for Id,another for course name.
Then use forech() for write <td> in view.html.
For example:
forech($array as $item){
echo('<td><a href="ProjectView.php?course_id=".$item["id"]>$item["name"]'</a></td>') ;
}

Sort a list by default under specific table in PHP

I currently have a long list that is being generated by PHP from a MySQL. It has 3 columns and the HTML for it looks roughly like:
<thead>
<tr>
<th style="display:none;">IP</th>
<th>ID</th><th>Status</th>
<th>Selection</th>
</tr>
</thead>
<tbody>
<?php while($a = $ks->fetch(PDO::FETCH_ASSOC)) { ?>
<tr>
<td style="display:none;"><?php echo $a['ip'];?></td>
<td><?php echo $a['id'];?></td>
<td><?php echo getstatus($a['status']);?></td>
<td><input type="checkbox" data-status="<?php echo getstatus($a['status']);?>" onclick="redes(this)" value="<?php echo $a['ip'];?>"></td>
</tr>
<?php }?>
Now, in every table column next to the TH text there are 2 arrows (one up and down) and you can sort them accordingly. I want to know how I can have one row sorted ASC by default when visiting the page?
eg.
<?php
$sort = $_GET['sort'] ?: 'ip';
$sql = 'SELECT ip,status,section FROM tab ORDER BY '. $sort;
$ks = $db->query($sql);

Auto sum total in search filter only - not the whole db

<table id="resultTable" data-responsive="table" style="text-align: left; width: 400px;" border="1" cellspacing="0" cellpadding="4">
<thead>
<tr>
<th> Total </th>
<th> Deposit </th>
</tr>
</thead>
<tbody>
<?php
$result = $db->prepare("SELECT * FROM student");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['contact']; ?></td>
</tr>
<?php
}
?>
</tbody>
<thead>
<tr>
<th> Total </th>
<th>
<?php
$results = $db->prepare("SELECT sum(contact) FROM student");
$results->execute();
for($i=0; $rows = $results->fetch(); $i++){
echo $rows['sum(contact)'];
}
?>
</th>
</tr>
</thead>
</table>
This is my code. I want to search a certain word only, and that item will generate the sum total, because right now it displays the whole sum of the row in the table. I want the search to generate the sum total can you help me?
While it's unclear what you're asking, here are the two scenarios I can imagine you want:
A count of rows matching "a certain word only":
$results = $db->prepare("SELECT sum(contact) FROM student");
First of all, sum is a GROUP BY Aggregate function. It's unlikely this is what you want in any run-of-the-mill scenario.
Change your query to this:
$results = $db->prepare("SELECT count(*) FROM student WHERE word = :word");
$results->execute( array( ":word" => "certain" ) );
This will count how many rows are returned when you have certain in the word column.
A sum of columns
Change your query to something like this:
$results = $db->prepare("SELECT ( number1 + number2 ) as totalNumber, * FROM student");
This will select every row and add an additional column to the result set called totalNumber that holds the sum (+) of the columns number1 and number2.

Sorting MYSQL results by categories using PHP

Here's the thing. I'm trying to sort my Comics collection. So far, I have two tables, one for the titles and type of comics (ID, prefix, title, type) and another one with individual information for each issue (ID, title, volume, issue number, value).
First thing I do is associate the title from the issues with the appropriate titles from the title table.
I do something like that:
SELECT * FROM comics, comicstitles WHERE comics.comTitle = comicstitles.titID ORDER BY comicstitles.titTitle ASC
What I get is a neat bunch of rows that I organize, and I get results like:
<table class='simple-list'>
<thead>
<tr>
<th>Title</th>
<th>Volume</th>
<th>Issue</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alpha Flight</td>
<td>1</td>
<td>1</td>
<td>$4.00</td>
</tr>
<tr>
<td>Alpha Flight</td>
<td>1</td>
<td>2</td>
<td>$3.00</td>
</tr>
<tr>
<td>Machine Man</td>
<td>2</td>
<td>1</td>
<td>$2.00</td>
</tr>
<tr>
<td>Wolverine</td>
<td>1</td>
<td>2</td>
<td>$35.00</td>
</tr>
<tr>
<td>Wolverine</td>
<td>1</td>
<td>1</td>
<td>$60.00</td>
</tr>
<tr>
<td>The X-Men</td>
<td>1</td>
<td>115</td>
<td>$100.00</td>
</tr>
<tr>
<td>The X-Men</td>
<td>1</td>
<td>116</td>
<td>$100.00</td>
</tr>
<tr>
<td>The X-Men</td>
<td>1</td>
<td>111</td>
<td>$100.00</td>
</tr>
<tr>
<td>The X-Men</td>
<td>1</td>
<td>114</td>
<td>$100.00</td>
</tr>
<tr>
<td>The X-Men</td>
<td>1</td>
<td>109</td>
<td>$160.00</td>
</tr>
<tr>
<td>The X-Men</td>
<td>1</td>
<td>112</td>
<td>$100.00</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan='4'><h3>Total: $764.00</h3></td>
</tr>
</tfoot>
Now, what I would like to do is a foreach, which would give me something like:
For each title, print the title once and all the individual issues underneath.
I would end up with something like:
TITLE
Issue number
Cover
Issue number
Cover
TITLE
Issue number
Cover
TITLE
Issue number
Cover
Issue number
Cover
Issue number
Cover
Issue number
Cover
I tried looking over and over, but I always end up on Wordpress, Joomla or Drupal sites.
And any other results is either WAY too abstract or uses numbers.
You can easily group the items visually by title by keeping track of the title and comparing it to the current one.
Something like (simple example):
// before your loop
$title = '';
// in your loop
if ($row['title'] === $title) {
// title already shown, do nothing or echo an
} else {
// new title, set and print it
$title = $row['title'];
echo htmlspecialchars($title);
}
You don't mention covers anywhere, so it's impossible to help with that until you've tried it, show the code and what the exact problem is.
There are several approaches for what you are trying to do. One approach is you also read the comic-id and then you "for-each" the result and put them in an array with the ID as Array-Index.
foreach ($rows as $row)
$arr[$row['titID']][] = $row;
If you do NOT have the need for further use of the data, you could just do
$active_id = $titID;
foreach ...
//if the current eached title-id has changed (not = $active_id)
//then set $active_id = new-title-id and start a new row
You could have an array in which each title has an array for itself.
$titles = array();
while ($row == $result->fetch_assoc()) {
if (array_key_exists($row['Title'], $titles)) {
array_push($titles[$row['Title']], $row);
}
else {
$titles[$row['Title']] = array($row);
}
}
foreach ($titles as $title=>$rows) {
echo $title . '<br>';
foreach ($rows as $row) {
echo $row['IssueNumber'] . '<br>' . $row['Cover'] . '<br>';
}
}
//Database Connection
mysql_connect("localhost", "username", "password") or die(mysql_error());
//Database selection
mysql_select_db("Database_Name") or die(mysql_error());
//your query
$data = mysql_query("SELECT * FROM comics, comicstitles WHERE comics.comTitle = comicstitles.titID ORDER BY comicstitles.titTitle ASC")
or die(mysql_error());
$temp_title="";
while($info = mysql_fetch_array( $data ))
{
$temp_title = $info['comicstitles'];
if(($temp_title == $info['comicstitles']))
{
echo "<tr>";
echo "<td>".$info[comics]."</td>";
echo "</tr>";
}
else
{
echo "<tr>"
echo "<td>".$info[comicstitles]."</td>"
echo "</tr>"
echo "<tr>";
echo "<td>".$info[comics]."</td>";
echo "</tr>";
}
}

PHP table creation with arrays

I have a table with a first column called: "Fruit", and a last column called: "Total"
And any columns in between are dynamically created by the number of students.
Below is what I'm looking to dynamically make, from what the database shows so far.
<table border="1">
<tr>
<th>Fruit</th>
<th>Sally</th>
<th>John</th>
<th>Total</th>
</tr>
<tr>
<td>apples</td>
<td>5</td>
<td>3</td>
<td>8</td>
</tr>
<tr>
<td>bananas</td>
<td>3</td>
<td>5</td>
<td>8</td>
</tr>
<tr>
<td>Oranges</td>
<td>3</td>
<td>3</td>
<td>6</td>
</tr>
<tr>
<td></td>
<td>11</td>
<td>11</td>
<td>22</td>
</tr>
</table>
Here's the difficulties I run into. The fruit names, students, and fruit consumed are pulled from the database.
And I don't know how many fruit rows nor student columns there will be.
This is as far as I got:
<table>
<tr>
<th>Fruit</th>
<?php $sqlS = db("SELECT s.*, sf.consumed FROM `tbl_students` s, `tbl_students_fruit` sf WHERE s.studentid = f.studentid ORDER BY s.studentname ASC");
while($student = mysql_fetch_array($sqlS)){ ?>
<th><?php echo $student['studentname'];?></th>
<?php } ?>
<th>Total</th>
</tr>
<?php $sqlF = db("SELECT * FROM `tbl_fruit` ORDER BY fruitname ASC");
while($fruit = mysql_fetch_array($sqlF)){ ?>
<tr>
<td><?php echo $fruit['fruitname'];?></th>
<td></td>
<td></td>
</tr>
<? } ?>
</table>
As you can see, I'm creating the Fruit rows, and Student columns. But it's incomplete. I've only created the column headers, and not the columns below the headers. From this point I am stuck on the guts of the table.
I am sure arrays are the way to go with this monstrosity. But the only way my feeble brain can make this work is to have more queries, which I'm sure is a very wrong way to do this.
If there were 3 students or 15 students, I can make them appear in the table th columns, but not in rows in their columns.
How does one traverse dynamic columns this way?
And if my demo above is confusing, I don't blame you!
well, I am assuming that your db structure is as follows:
[tbl_students]
studentid, studentname
[tbl_fruit]
fruitid, fruitname
[tbl_students_fruit]
id, fruitid, studentid, consumed
http://pastebin.com/CxPUeXR0
I haven't tested it, so good luck

Categories