PHP: How to activate class "active" in pagination, If $_GET['page']==$pagenumber - php

This is my pagination code:
<?php
while($pagenumber != $totalpages)
{
echo"<a href='?page=" . ++$pagenumber. "' >" . $pagenumber. "</a> ";
}
?>
The above code will look like this in normal html:
1
2
I want to add class="active" with this condition
If ($_GET['page'] == " . $pagenumber . ")
echo "class="active" ";
I do not know, how to insert if statement inside echo command.
`

Here you are:
<?php
while ($pagenumber != $totalpages) {
++$pagenumber;
$class = ($_GET['page'] == $pagenumber) ? 'active' : '';
echo "<a href='?page={$pagenumber}' class='{$class}'>{$pagenumber}</a>";
}

I think this might help:
if ($_GET['page'] == $pagenumber) echo "<a href='?page=$pagenumber' class='active'>$pagenumber</a>";

Please try:
<?php
while($pagenumber != $totalpages) {
$active = ($_GET['page'] == $pagenumber) ? ' class="active"' : '' ;
echo '<a href="?page='.++$pagenumber.'"'.$active.'>'.$pagenumber.'</a>';
}
?>
You will have a very clean HTML code without the class attribute empty on the non-active elements.

Related

How do i pass a div id into a php pagination url here is my code example

I have a div id=example in html the url would be www.mysite.com#example. Now i want to refer that in php url below with index#example
<?php
if($page <=1 )
{
echo '<span id="page_links" style="font-weight:bold;"> || </span> ';
}
else
{
$j = $page- 1;
echo '<span><a id="page_a_link" href="index?&page=' . $j . '" style="color:#ac5f42"> Prev </a></span> ';
}
for($i=1; $i <= $total_pages; $i++)
{
if($i<>$page)
{
echo '<span>' . $i . '</span>  ';
}
else
{
echo '<span id="page_links" style="font-weight:bold;">' . $i . '</span> ';
}
}
if($page == $total_pages )
{
echo '<span id="page_links" style="font-weight:bold;">||</span> ';
}
else
{
$j = $page + 1;
echo '<span> Next </span>';
}
?>
I got this somewhere here on stackoverflow and it answered my question perfectly
<script>
if (window.location.hash == "") {
window.location = window.location + "#sectionid";
}
</script>
my url became Next
Thanks Stackoverflow after weeks of looking for solution!!!!

how can i echo two different record from mysql inside while loop

im trying to echo out two different column inside a while loop and the output will be inside <td>
here is my code:
while($row = mysql_fetch_array($stmt)){
$doc_number = $row['doc_number'];
$place_of_issue = $row['place_of_issue'];
$crew_rank_code = $row['crew_rank_code'];
$full_name = $row['full_name'];
$date_of_birth = $row['date_of_birth'];
$place_of_birth = $row['place_of_birth'];
$date_issue = $row['date_issue'];
$date_expiry = $row['date_expiry'];
$place_of_birth = $row['place_of_birth'];
echo '<tr>';
echo "<td>$crew_rank_code</td>";
echo "<td>$full_name</td>";
echo "<td>$date_of_birth</td>";
echo "<td>$place_of_birth</td>";
echo "<td>". (($row['doc_type'] == '1') ? "$doc_number" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '1') ? "$date_issue" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '1') ? "$date_expiry" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '1') ? "$place_of_issue" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '2') ? "$doc_number" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '2') ? "$date_issue" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '2') ? "$date_expiry" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '2') ? "$place_of_issue" : "") . "</td>";
echo '</tr>';
}
right now, the code gives me an output up to PASSPORT PLACE ISSUE column but after that, my code echo "<td>". (($row['doc_type'] == '2') ? "$doc_number" : "") . "</td>"; is no longer working
how can I make the echo "<td>". (($row['doc_type'] == '2') ? "$doc_number" : "") . "</td>"; working?
thank in advance guys
EDIT:
i added mysql
$stmt = mysql_query("select * from info join crew_documents_table on info.id = crew_documents_table.document_crew_id join crew_rank on info.crew_rank = crew_rank.crew_rank_id where crew_rank in ('1','2','3') and crew_status = '$crew_status' and vessel = '$vessel_name' and date_issue in (select max( date_issue ) from crew_documents_table where crew_documents_table.document_crew_id = info.id)")or die(mysql_error());
edit: Sorry my last answer was wrong, you should maybe rewrite your sql to end up with something like this
echo "<td>". $row['doc_type'][1]['$doc_number'] . "</td>";
Ternary operators do not necessarily need () braces, just a first note on your readability of code. Moving on, have you yet debugged what the row doc_type actually contains by doing a simple output of its content? Your condition:
echo '<td>' . (int)$row['doc_type'] == 2 ? $doc_number : '' . '</td>';
Will only return the $doc_number, aka. $row['doc_number'], if the doc_type is set as 2. Also, for this, always check numerical values as data-type int. It will automatically parse the doc_type in the condition with the delimiter (int).
Ternary expressions are brilliant, but in your case, you're pro-longing it. For readability you could simply use conditional expressions and short-hand operators to output:
<?php
while($r = mysql_fetch_array($stmt)):
$doc_number = $r['doc_number'];
/* [...] */
if( (int) $r['doc_type'] == 1 ): ?>
<td> <?= $doc_number ?> </td>
<!-- [...] -->
<?php else if( (int)$r['doc_type'] == 2 ): ?>
<td> <?= $doc_XXXX ?> </td>
<!-- [...] -->
<?php else:
// debug console to check what `doc_type` value was if no condition was met
echo '<script>console.log("[DOC_TYPE_DEBUG] ' . $r['doc_type'] . '");</script>';
endif;
endwhile;

How do active class on pagination

I'm trying to active class on list
Kindly if any one can help me to add class="active" on displayed pagination page:
$perpage= $conf['perpage'];
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
};
$start_from = ($page-1) * $perpage;
$result = "SELECT * FROM topics LIMIT $start_from, $perpage";
$result = mysql_query ($result);
$n = 0;
while ($row = mysql_fetch_array ($result)){
echo '<tr>';
echo '<td>'.$row['topic_no'].'</td>';
echo '</tr>';
++$n;
}
$sql = "SELECT * FROM topics";
$result = mysql_query($sql);
$total_records = mysql_num_rows($result);
$total_pages = ceil($total_records / $perpage);
echo '<ul class="pagination">';
echo "<li><a href='topics.php?page=1'>".'<'."</a></li> ";
for ($i=1; $i<=$total_pages; $i++) {
echo "<li ><a href='topics.php?page=".$i."'>".$i."</a></li> ";
};
echo "<li><a href='topics.php?page=$total_pages'>".'>'."</a> </li>";
echo '</ul> ';
Thanks in Advance
You Can also try this code. it's working
$c="active";
for ($i=1; $i <$total_page ; $i++) {
if($page==$i)
{
$c="active";
}
else
{
$c="";
}
echo "<li class=\"$c\">$i</li>";
}
$active = $i == $page ? 'class="active"' : '';
echo "<li ><a {$active} href=\"topics.php?page={$i}\">{$i}</a></li> ";
We just need to add class="active" only if page is current, otherwise we add nothing. If you already have classes for rows - you just need to use smth like
$activeClass = $i == $page ? 'active' : '';
echo "<li ><a class=\"my-row-class1 {$activeClass}\" href=\"topics.php?page={$i}\">{$i}</a></li> ";
echo "<li if($_GET['page']==$i){ class='active'}><a href='topics.php?page=".$i."'>".$i."</a></li> ";
Please try this. It might help you
$search = #$_GET['page']; // get value form other page
$page ='A';
for ($Page=1; $Page <27 ; $Page++) { ?> // abcdef... create
<li class="<?php if($search==$page){ echo 'active'; } ?>"> <?php echo ''. $page++ . '' ;?> </li>
<?php } ?>
/* if($search==$page){ echo 'active'; } this code means if search value == alphabet then class active call automatically */

using "href" in if statement

I am new in the world of web development.
I am trying to redirect the user to a link based on the some conditions using IF statement.
but the inside the IF block href is not being recognized.
Any advise so as how can I accomplish this ?
Thanks
EDIT 1
<?php if ($len == 12){
track;
}
else {
track;
}
?>
Use concatenation. It sure makes things easier.
<?php
$url1 = 'track';
$url2 = 'track';
if ($len == 12) {
echo $url1;
} else {
echo $url2;
}
?>
try this:
<?php
if ($len == 12){
echo "<a href='http://www.dddd.in/" . $row_Recordset1['TRACKING_NO'] . "1213'> track </a>";
} else {
echo "<a href='http://www.aaa.in/" . $row_Recordset1['TRACKING_NO'] . "1231'> track </a>";
}
?>

elseif statement not working

I have written some script that works great, BUT I have tried to add an elseif statement in which appears to do absolutely nothing, can't figure out where I am going wrong. (Yes I am a PHP newbie, but I am trying! :)
$i = 0;
foreach($arrJobs as $job)
{
echo "<div class=\"job-ind\">\n";
echo '<p>' . $job->title . ', ' . $job->location . "</p>\n";
echo '<p class="smaller">Reference: ' . $job->reference . "</p>\n";
echo '<p><strong>' . $job->salary . "</strong></p>\n";
echo "</div>\n";
if (++$i == 5) break;
elseif ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
}
Everything up the elseif statement works perfectly.
Must be something simple I am missing or misunderstanding!
$i will never be 0 at this location.
You increase it in the if-statement before the test (++$i).
Note that elseif and else if will only be considered exactly the same when using curly brackets.
if (++$i == 5) break;
elseif ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
Won't work, change it to:
if ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
elseif (++$i == 5) {
break;
}
elseif vs. else if
I see a lot of people discussing this in the thread.
These are both correct and will both work.
The difference being :
else if() {
}
Translates to:
else {
if(){
}
}
And can't be used in code blocks using colons like this:
if:
stuff
else if()://won't work
//stuff
endif;
basic syntax error ? you can't use the non braced short if's when doing an else if
if (++$i == 5) {
break;
} else if ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
notice the colour highlighting on this block compared to yours
Here is the working outcome, thanks for all your help!
$i = 0;
foreach($arrJobs as $job)
{
echo "<div class=\"job-ind\">\n";
echo '<p>' . $job->title . ', ' . $job->location . "</p>\n";
echo '<p class="smaller">Reference: ' . $job->reference . "</p>\n";
echo '<p><strong>' . $job->salary . "</strong></p>\n";
echo "</div>\n";
}
if ($i = 0) {
echo "<div class=\"job-ind\">\n";
echo '<img src="img/no-vac.png"/>';
echo "</div>\n";
}
else if (++$i == 5) {
break;
}

Categories