Display current category name - php

Thanks in advance.
I am trying to display the name of the current category which I'm in. Nothing that I've been trying for 4 hours now has worked..
My code:
if($catList) {
$c = 0;
while($category = $catList->fetch(PDO::FETCH_ASSOC)){
$margintop = 38*$c;
if($category['id'] == $thiscategory) {
echo "<a href='".$link."&category=".$category['id']."'>
<div class='category_name'>".strip_tags($category['name']). "</div></a></div>";
} else {
echo "<a href='".$link."&category=".$category['id']."'>
<div class='category_name'>".strip_tags($category['name']). "</div></a></div>";
}
$c++;
}
} else {
echo "No categories to show.";
}
what I've tried to get the current category name, in a different area in the page:
if($category['id'] == $thiscategory) {
echo ".strip_tags($category[name])." ;
}
which gives a blank result.

You need:
Compare with $category (instead of $thiscategory), because you set in link &category=
You need to show some difference if value is selected
if($catList) {
$c = 0;
while($myCategory = $catList->fetch(PDO::FETCH_ASSOC)){
$margintop = 38*$c;
if($myCategory['id'] == $category) {
echo "<a href='".$link."&category=".$myCategory['id']."' class=\"active\">
<div class='category_name'><strong>".strip_tags($myCategory['name']). "</strong></div></a></div>";
} else {
echo "<a href='".$link."&category=".$myCategory['id']."'>
<div class='category_name'>".strip_tags($myCategory['name']). "</div></a></div>";
}
$c++;
}
} else {
echo "No categories to show.";
}

Hey you may try single_cat_title()
Source: https://developer.wordpress.org/reference/functions/single_cat_title/

Related

Display username according to number

I have database table with users. There is one column called fkhouseid which can be (1, 2, 3, 4, 5) it connects user to the house which leads to another table with house names.
I am trying to print each user in separate column for each house. Here is first query to get users:
$numbers = '12345';
$unityResult = getUnityEmail();
Then get number of houses:
$houseResult = getHouses();
$count = mysqli_num_rows($houseResult);
There are 5 houses in total. Then I am trying to print names for each house:
for ($x=0;$x<$count;$x++){
echo "<div id='memberList$x' style='display:inline-block;vertical-align:top'>";
echo "<fieldset class=fieldname style='display:inline'>";
if( $numbers[$x] == 1){ echo "<legend class=name_legend>u</legend>"; }
else if( $numbers[$x] == 2){ echo "<legend class=name_legend>n</legend>"; }
else if( $numbers[$x] == 3){ echo "<legend class=name_legend>i</legend>"; }
else if( $numbers[$x] == 4){ echo "<legend class=name_legend>t</legend>"; }
else{ echo "<legend class=name_legend>y</legend>"; }
echo "<ul class='list'>";
while ($row_unity = mysqli_fetch_array($unityResult)) {
if ($row_unity['fkhouseid'] == $numbers[$x]) {
format_member_list_filters( $row_unity);
}
}
echo "</ul>";
echo "</fieldset>";
echo "<br>";
echo "</div>";
mysqli_data_seek($houseResult, 0);
}
I am to get houses(each letter for each house) but I am not able to get names according their houses:
How I can make it display names for other columns too?
The problem was easy to solve.
mysqli_data_seek($houseResult, 0);
Changed to
mysqli_data_seek($unityResult, 0);

PHP if - else - else statement

<html>
<input type='text' name='mobile phone' value='
<?php if (strpos($phone_number, '07') === 0) {
echo $phone_number;
} else {
echo $alt_phone;
}?>'
</html>
Works fine. I would like to combine the above with:
<?php if (!empty($alt_phone)) {
echo $alt_phone;
} else {
echo '07777777777';
}?>'`
I have tried ELSEIF with the new condition, and a completely separate <?php ?> section and both times I get a blank page, instead of a textbox with a telephone number in it.
I am trying to achieve this: If $phone_number is a mobile, enter this number, otherwise enter the alt_phone, unless $alt_phone is blank, then enter '07777777777'.
try
<?php
if (!empty($phone_number)) {
echo $phone_number;
}
elseif(!empty($alt_phone))
{
echo $alt_phone;
}
else{
echo '07777777777';
}
?>'`
This will do the trick
<?php
if (strpos($phone_number, '07') === 0) {
echo $phone_number;
}
else if (!empty($alt_phone)) {
echo $alt_phone;
}
else {
echo '07777777777';
}
?>

If statement within echo?

I was wondering if it's possible to have an if statement within an echo.
I have if statement which works fine when echoing results through the a while loop... This is the statement:
<div><?php if ($row['image'] == '') {}
else {echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";} ?>
<?php if ($row['video'] == '') {}
else {echo "<iframe src={$row['video']}></iframe>";} ?></div>`
So basically it's either a video or an image which works fine but then I implemented an infinite scroll to my blog which echoes the data from the database through and if statement like so:
if ($results) {
while($obj = $results->fetch_object())
{
echo '
<div><h3>'.$obj->headline.'</h3> </div>
<div><img src='data:image/jpeg;base64,".base64_encode('.$obj->image.')."'></div>'
So I wondering if anyone knows if it's possible to transfer that if statement within this echo so that it display an image firstly and then knows whether one is present or when a video is present within the database.
Thanks in advance for any help.
PS: I'm very new to coding/php!
Of course. Just split up the echo into multiple statements:
while($row = $results->fetch_object()) {
echo '<div>';
if ($row['image'] == '') {
} else {
echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";
}
if ($row['video'] == '') {
} else {
echo "<iframe src={$row['video']}></iframe>";
}
echo '</div>';
}
Try this one.
//first initialize a variable as a string
$result="";
while($obj = $results->fetch_object()) {
$result.="<div>";
if (!empty($obj['image'])){
$result.="<img src='data:image/jpeg;base64,".base64_encode($obj['image'])."'>";
}
elseif (!empty($obj['video'])){
$result.="<iframe src={$obj['video']}></iframe>";
}else{
//show some notification or leave it
//echo 'not Found';
}
$result.="</div>";
}
//finally you need to print the result variable.
echo $result;

Having ULs for foreach loops

Couple of issues here I'm not sure on what the issue is but with how my coding is set up it looks odd in the firebug with how the uls, lis, and h2s are rendered. Any ideas why and also I need some suggestions on how to fix the errors I am getting which is caused by some categories not having child pages. In which case if they don't have child pages I don't want my code to do anything just pass over it.
kansasoutlawwrestling.com/site-map
<?php
echo "<pre>";
print_r($categoriesArray);
echo "</pre>";
if((isset($categoriesArray)) AND ((!empty($categoriesArray))||($categoriesArray !== NULL)))
{
if(count($categoriesArray) <= 0)
{
echo "There are no content pages on this site!";
}
else
{
foreach ($categoriesArray as $row)
{
echo "<h2>".stripslashes($row['name'])."</h2>";
echo "<ul>";
foreach ($row['children'] as $row2)
{
echo "<li>".stripslashes($row2['link_name'])."</li>";
if (count($row2) > 0)
{
echo "<ul>";
foreach ($row2['child_pages'] as $row3)
{
echo "<li>".stripslashes($row3['link_name'])."</li>";
}
echo "<ul>";
}
}
echo "</ul>";
}
}
}
else
{
echo "There are no content pages on this site!";
}
?>
EDIT: Any other ideas for me to try?
You could do;
if(count($row2['child_pages']) < 1) {
continue;
}
Hope it helps

Codeigniter validation

I have many goals to be printed on the screen.
but it shows error when i use it like this
echo $this->validation->rshort_goal.$i;
What is the right way to use this?
if($sgoal !='')
{
$scount = count($sgoal);
$i =1;
foreach($sgoal as $row)
{
<textarea name="rshort_goal<?php print $i;?>" id="short_goal" class="short_go">
<?php if($this->validation->rshort_goal.$i)
{
echo $this->validation->rshort_goal.$i;
}
elseif($this->validation->rshort_goal.$i._error !='')
{ echo ''; }
else
{echo $$row->goal_description; }
?>
</textarea>
<?php
$i++;
}
}
echo #$this->validation->{'rshort_goal'.$i};
Perhaps you want to call a function like this?
call_user_func($this->validation, 'rshort_goal' . $i);

Categories