Codeigniter validation - php

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);

Related

Display current category name

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/

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';
}
?>

how to append numbers into string in php

I have a for loop like this in php
<?php
for($i=0;$i<=100;$i++)
{
echo $i;
}
What I need is, I want to generate roll number using this for loop like this.
SN0001
SN0002
SN0003
.....
SN0010 not SN00010
......
SN0100 not SN000100
I have tried this
<?php
for($i=0;$i<=100;$i++) {
$str='SN00'.$i;
echo $str;
}
?>
Note this (SN0010 and SN0100). I need SN0010,SN0100 not SN00010, SN000100.
What should I do for that?
You can use str_pad() function:
<?php
for($i=0;$i<=100;$i++) {
echo 'SN' . str_pad($i, 4, STR_PAD_LEFT, '0');
}
?>
Please try this code-
for($i=0;$i<=100;$i++) {
echo 'SN' .sprintf("%'.04d\n", $i);
}
for($i=0;$i<=100;$i++)
{
if($i<=9){
$zeroValue = '000';
}
elseif($i>9 && $i<=99){
$zeroValue = '00';
}
elseif($i>99 && $i<=999){
$zeroValue = '0';
}
echo 'SN'.$zeroValue.$i.'<br>';
}
<?php
for($i=0;$i<=100;$i++) {
$str='SN00'.$i;
if(strlen($str)<6)
{
$str='SN000'.$i;
}
if(strlen($str)>6)
{
$str='SN0'.$i;
}
echo $str;
}
?>

How to speedup by code?

is there a way to speed up my code? It takes about 15 seconds to load ... I don't really see a way to reduce my code... I just thought about inserting the values into database, so the user does not have to load new info every time.. but the thing is that my cron only allows 1 load per hour ... by loading new info on every load it gives me fresh information..
$q1=mysql_query("SELECT * FROM isara");
while($r1=mysql_fetch_array($q1)){
$named=$r1['name'];
$idd=$r1['id'];
$descd=$r1['desc'];
$online=check_online($named);
$char = new Character($r1['name'],$r1['id'],$r1['desc']);
if($online == "online"){
$char->rank = $i++;
}
else{
$char->rank = 0;
}
$arr[] = $char;
}
?>
<br />
<h2 style="color:green">Online enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank>=1){
echo "<a style=\"color:green\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
<br />
<h2 style="color:red">Offline enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank==0){
echo "<a style=\"color:red\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
As I wrote in the comment, fetch the page once instead of once for every name in the database.
Pseudo code for my comment:
users = <get users from database>
webpage = <get webpage contents>
for (user in users)
<check if user exists in webpage>
As mentioned in the comments you're calling a webpage for each entry in your database, assuming that's about 2 seconds per call it's going to slow you down a lot.
Why don't you call the page once and pass the contents of it into the check_online() function as a parameter so your code would look something like this which will speed it up by quite a few magnitudes:
$content=file_get_contents("http://www.tibia.com/community/?subtopic=worlds&worl‌​d=Isara",0);
$q1=mysql_query("SELECT * FROM isara");
while($r1=mysql_fetch_array($q1)){
$named=$r1['name'];
$idd=$r1['id'];
$descd=$r1['desc'];
$online=check_online($named,$content);
$char = new Character($r1['name'],$r1['id'],$r1['desc']);
if($online == "online"){
$char->rank = $i++;
}
else{
$char->rank = 0;
}
$arr[] = $char;
}
?>
<br />
<h2 style="color:green">Online enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank>=1){
echo "<a style=\"color:green\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
<br />
<h2 style="color:red">Offline enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank==0){
echo "<a style=\"color:red\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
and your check_online() function would look something like this:
function check_online($name,$content){
$count=substr_count($name, " ");
if($count > 0){ $ex=explode(" ",$name); $namez=$ex[1]; $nameused=$namez; }
else{ $nameused=$name; }
if(preg_match("/$nameused/",$content)){ $status="online"; }
else{ $status="offline"; }
return $status;
}
You can also do the following to make it faster
Stop using select * which is very bad on innodb
Put better indexes on your database to make the recordset return faster
Install PHP 5.4 as it's faster especially as you're creating a new object in each iteration
Use a byte code accelerator/cache such as xdebug
you should avoid using distinct (*) keyword in your SQL Query
for more information read this http://blog.sqlauthority.com/category/sql-coding-standards/page/2/

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

Categories