I need some help regarding pagination, the problem is that I want to show my database records first time on button click with pagination but every time I click on pagination link it requires click on button
Here is my code I know I am making some mistake please check and kindly do me a favor by correcting my mistake ..
if (isset($_POST["search"])){
$result=mysql_query("select count(*) from reg_phone");
$row=mysql_fetch_row($result);
$tr=$row[0];
$rpp=4;
$pn=1;
if(isset($_GET['pn']))
{
$pn=$_GET['pn'];
}
$tp=($tr/$rpp);
if($tr/$rpp>=0)
{
$tp++;
}
$from=(($pn-1)*$rpp)+1;
$to=($pn)*($rpp);
$show = "SELECT * FROM reg_phone where Id between $from and $to";
$rs = mysql_query($show) or die(mysql_error());
#****results in Grid****
echo "<table width='100%' border='1' cellpadding='2' border-color='#000' id='tbl'>";
echo "<tr>";
echo "<td style='background: white;'><b>IMEI #</td>";
echo "<td style='background: white;'><b>Phone #</td>";
echo "<td style='background: white;'><b>From Date</td>";
echo "<td style='background: white;'><b>Status</td>";
echo "</tr>";
$rowID = 1;
while ($row = mysql_fetch_array($rs)) {
echo "<tr>";
echo "<td id='imeinum" . $rowID . "'>$row[imei]</td>";
echo "<td id='phnum" . $rowID . "'>$row[phonenum]</td>";
echo "<td id='datepicker" . $rowID . "'>$row[fdate]</td>";
echo "<td id='rad" . $rowID . "'>$row[status]</td>";
echo "</tr>";
$rowID++;
}
echo "</table>";
echo "<ul id='pages'>";
for($i=1;$i<=$tp;$i++)
{
echo "<li><a href='phonereg.php?pn=$i'>$i</a></li>";
}
echo "</ul>";
#**********************
mysql_free_result($rs);
}
http://www.hscripts.com/scripts/jquery/pagination.php
the script here is really helpful to me. Exactly what i want to do. hope anyone one in future will need this.
thanks all for suggestions.
Regards.
BKay
Related
I've got a table in Drupal with the following code:
$db = mysql_connect("localhost", "root", "moocow");
mysql_select_db("vedb", $db);
$result = mysql_query("SELECT NodeID,NodeDesc,NodeZone,Fusion,DSLID FROM `nodeidtable` WHERE DSLID != '' AND `NodeZone` = 'CLOSED' ORDER BY NodeID ASC");
$num_rows = mysql_num_rows($result);
echo "<table>";
echo "<tr>";
echo "<th>CLOSED SITES</th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "</tr>";
echo "<tr>";
echo "<th>Node ID</th>";
echo "<th>Node Address</th>";
echo "<th>Node Zone</th>";
echo "<th>Fusion Status</th>";
echo "<th>Service Number</th>";
echo "</tr>";
//display the data
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
echo "<br>";
echo "<tr>";
echo "</table>";
mysql_free_result($result);
mysql_close($db);
?>
Now I can change it renders the td to include the individual columns, but I really want to add a little edit button on the right-hand side which will let me edit that particular row fields.
Any ideas?
Replace your while loop with the following code:
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
//create link for current node edit
echo "<td align='center'>". l(t('Edit this node'), 'node/' . $row['NodeId'] . '/edit') ."</td>";
echo "</tr>";
}
Remove echo "<br>"; and echo "<tr>"; below to while loop. This will resolve the issue for you
This is my code (horrible one):
<?php
include 'connect/con.php';
$result = mysqli_query($con,"SELECT id, vidTitle FROM newsvid");
$result1 = mysqli_query($con,"SELECT imgCover, vidSD FROM newsvid");
$result2 = mysqli_query($con,"SELECT published FROM newsvid");
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result1)) {
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td >" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
mysqli_close($con);
?>
</body>
</html>
The question is how to show data from database in this layout:
--------------------------------
-id----------vidTitle-----------
--------------------------------
-imgCover------vidSD------------
--------------------------------
----------published-------------
So every time I will add more data , another block like I showed before will add up under existing one.
........................................................................................
There's no need to write 3 queries. You could do that with only one select, and then put all the echos inside a while. That way you're writing, it would run all the ids and titles first, then it would put a table after the table, with cover and vidSD.
Try to make a single query:
SELECT id, vidTitle, imgCover, vidSD, published FROM newsvid
That way you will have, on each row returned from database, all the information about the same row.
Now, running a while is the same as you're doing, just adapting some HTML:
echo "<table width='600' border='1'><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'>" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
You may want to order it too. Adding ORDER BY id DESC, would do that.
I'm getting all rows from mysql database. Now I want to highlight only first row in php while loop with class name keywordHighlight.
How do I highlight only first row in php while loop result ?
if($numSearch > 0){
echo "<font color='green'>We found $numSearch result(s).</font>";
echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<td class='' valign='top' width='200'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($resGetSearch = mysql_fetch_array($getSearch)){
$SearchCdid = $resGetSearch['cdid'];
$SearchFamilyName = $resGetSearch['family_name'];
$SearchGivenName = $resGetSearch['given_name'];
$SearchCompamyCid = $resGetSearch['cid'];
$SearchDepartment = $resGetSearch['department'];
$SearchTitle = $resGetSearch['title'];
$SearchComapnyName = mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
$resSearchCompanyName = mysql_fetch_array($SearchComapnyName);
$companyName = $resSearchCompanyName['company_name'];
if (strlen($companyName) >= 20) {
$companyName = substr($companyName, 0, 10). "" . substr($companyName, -5);
}else{
$companyName = $companyName;
}
// my Highlighted class = keywordHighlight";
echo "<tr onclick='getDetails($SearchCdid);' >";
echo "<td valign='top' >$companyName</td>";
echo "<td valign='top'>$SearchFamilyName</td>";
echo "<td valign='top'>$SearchGivenName</td>";
echo "<td valign='top'>$SearchDepartment</td>";
echo "<td valign='top'>$SearchTitle</td>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr/>";
echo "<br/>";
}//elseif is not empty search
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
Do it like this
$i = 1;
while($resGetSearch = mysql_fetch_array($getSearch)){
$highlight = $i == 1 ? 'keywordHighlight' : '';
echo "<tr class='{$highlight}' onclick='getDetails($SearchCdid);' >";
---------------
-------------
-------------
$i++;
}
Only with CSS
or you can highlight it only with css
#highlight tbody tr:nth-child(1){
background: #ff6600;
}
There is more and elegant way to highlight only first row with only css not need to code, consider the example http://jsbin.com/soravuzakahu/1/
You could just add a boolean outside the loop. Like so:
$first = true;
while($resGetSearch = mysql_fetch_array($getSearch)){
if(first == true){
// Add code here that only applies to the first iteration.
}
$first = false;
}
<?php
if($numSearch > 0){
echo "<font color='green'>We found $numSearch result(s).</font>";
echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<td class='' valign='top' width='200'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
$i = 0;
while($resGetSearch = mysql_fetch_array($getSearch)){
++$i;
$SearchCdid = $resGetSearch['cdid'];
$SearchFamilyName = $resGetSearch['family_name'];
$SearchGivenName = $resGetSearch['given_name'];
$SearchCompamyCid = $resGetSearch['cid'];
$SearchDepartment = $resGetSearch['department'];
$SearchTitle = $resGetSearch['title'];
$SearchComapnyName = mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
$resSearchCompanyName = mysql_fetch_array($SearchComapnyName);
$companyName = $resSearchCompanyName['company_name'];
if (strlen($companyName) >= 20) {
$companyName = substr($companyName, 0, 10). "" . substr($companyName, -5);
}else{
$companyName = $companyName;
}
// my Highlighted class = keywordHighlight";
if($i == 1)
echo "<tr onclick='getDetails($SearchCdid);' >";
else
echo "<tr class='keywordHighlight' onclick='getDetails($SearchCdid);' >";
echo "<td valign='top' >$companyName</td>";
echo "<td valign='top'>$SearchFamilyName</td>";
echo "<td valign='top'>$SearchGivenName</td>";
echo "<td valign='top'>$SearchDepartment</td>";
echo "<td valign='top'>$SearchTitle</td>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr/>";
echo "<br/>";
}//elseif is not empty search
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
Use a flag and set its value to true and while in loop check its value , if its true then print class name and set its value to false. Like $flag=true; then inside while loop check
if($flag==true) {
<td class='yourclassname';
$flag= false;
}
put some flag $f=0;
if f==0 then do something like this:
$highlight="<div class='keywordHighlight'>valur of first row</div>";// instead of dive you can use table also it depends on how you want to display.
$f=$f+1;
and rest in else part.
$first = true;
while ( $resGetSearch = mysql_fetch_array($getSearch) )
{
if ( $first == true )
{
// Add code here that only applies to the first iteration.
$first = false;
}
else
{
// Add code here that only applies to the 2, 3, and so on.
}
}
hello everyone i have made a mysql table which consist of two columns and 35 rows,
now problem is i want to span or merge some of the columns in it but i don't know how to do it
here is my code
$result = mysqli_query($con,"SELECT * FROM Spec");
echo "<table border='1' id='spec'>
<tr>
<th class='th'>Engine</th>
<th class='th'>Spec</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='td1'>" . $row['Engine'] . "</td>";
echo "<td class='td'>" . $row['Spec'] . "</td>";
echo "</tr>";
}
if($row['Engine'] = $row['Suspension'])
{
echo "<td colspan='2'>" . $row['Suspension'] . "</td>";
}
echo "</table>";
mysqli_close($con);
There is problem with and in while loop, please update it accordingly.
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='td1'>" . $row['Engine'] . "</td>";
echo "<td class='td'>" . $row['Spec'] . "</td>";
echo "</tr>";
}
if($row['Engine'] = $row['Suspension'])
{
echo "<tr>";
echo "<td colspan='2'>" . $row['Suspension'] . "</td>";
echo "</tr>";
}
echo "</table>";
Several problems with your logic there and a couple of code issues. Since you haven't explained exactly what format your data is in and what layout you want to achieve I'll have a bash at both versions you can test them and see which one fits the layout you're after.
This version fixes the IF statement to correctly use comparitive operator of == not assignment of =
It also creates a new table row if the engine and suspension column values match so you'd end up with something looking like this
| ENGINE | SPEC |
| SUSPENSION |
$result = mysqli_query($con,"SELECT * FROM Spec");
echo "<table border='1' id='spec'>
<tr>
<th class='th'>Engine</th>
<th class='th'>Spec</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td class='td1'>" . $row['Engine'] . "</td>";
echo "<td class='td'>" . $row['Spec'] . "</td>";
echo "</tr>";
if($row['Engine'] == $row['Suspension']) {
echo "<tr><td colspan='2'>" . $row['Suspension'] . "</td></tr>";
}
}
echo "</table>";
mysqli_close($con);
Of course it relies that the contents of your Suspension column are exactly the same as the contents of your Engine column which I'll place a bet on they don't but you'll have to address you're logic about that
The other way of doing it is like this. This version will show | ENGINE | SPEC | or if your engine column matches your Suspension column it'll just show the | SUSPENSION | column instead.
$result = mysqli_query($con,"SELECT * FROM Spec");
echo "<table border='1' id='spec'>
<tr>
<th class='th'>Engine</th>
<th class='th'>Spec</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
if($row['Engine'] == $row['Suspension']) {
echo "<tr><td colspan='2'>" . $row['Suspension'] . "</td></tr>";
} else {
echo "<tr>";
echo "<td class='td1'>" . $row['Engine'] . "</td>";
echo "<td class='td'>" . $row['Spec'] . "</td>";
echo "</tr>";
}
}
echo "</table>";
mysqli_close($con);
Both will work though I suspect neither will provide you what you're looking for personally I don't think you need the IF() statement at all
I have been trying to work this out for hours and I’m not sure how to achieve the correct result, hopefully someone will be able to help me.
I have the below code that will echo the result is a table vertically, but I want the cells next to each other horizontally, how can I achieve this?
<?php
echo "<table border='1' cellpadding='1' width='100%' bordercolor='000099'border='solid'>
";
echo '<div style="width:100%;">';
while($row = mysql_fetch_array($boxlink))
echo "<tr>";
{
echo "<td>" . $row['page_page_title'] . "</td>";
}
echo "</tr>";
echo "</table>";
echo '</div>';
?>
Brill that worked thanks a lot, why is it the small things can cause such a problem!
Move your tr tags outside of the loop. Each time a tr tag is seen it makes another row.
try this:
<?php
echo '<div style="width:100%;">';
echo "<table border='1' cellpadding='1' width='100%' bordercolor='000099'border='solid'>
";
echo "<tr>";
while($row = mysql_fetch_array($boxlink))
{
echo "<td>" . $row['page_page_title'] . "</td>";
}
echo "</tr>";
echo "</table>";
echo '</div>';
?>