How to have a if statment inside if - php

I have two rank in my forum Admin = 1 and User = 0 Then this code is doing so the admin get red name.
if($info['rank'] == 1) { $x= "<a class='admin'>".$last_user."</a>";
} else { $x= "<a class='user'>".$last_user."</a>"; }
But everyone is getting red name..

use the $x where you want to wish.
<?PHP
if (mysql_num_rows($res) > 0) {
while ($row = mysql_fetch_assoc($res)) {
if($info[rank] == 1) { $x= "<div class='admin'>".$last_user."</div>"; } else { $x="<div>".$last_user."</div>"; }
$tid = $row['id'];
$cid = $row['category_id'];
$title = $row['topic_title'];
$views = $row['topic_views'];
$date = $row['topic_date'];
$creator = $row['topic_creator'];
if ($row['topic_last_user']== "") { $last_user = getusername($row['topic_creator']); } else { $last_user = getusername($row['topic_last_user']); }
if ($row['topic_last_user']== "") { $last2_user = 'Started by'; } else { $last2_user = 'Most recent by'; }
$topics .="
<li class='category'><div class='left_cat'>
<a class='underline' href='view_topic.php?cid=".$cid."&tid=".$tid."'>
<div class='title'><i class='icon-comment'></i> ".$title."</div></a>
<div class='info'><i class='icon-comments icon-1'></i> ".topicreplies($cid, $tid)." Comments <i class='icon-user icon-1'>
</i>".$last2_user."
$x
<i class='icon-calendar'> </i> ".convertdate($date)."</div>
</div>";
$topics .= "<div class='right_cat'>
<div class='face'>
<img class='img_face' src='https://minotar.net/avatar/".getusername($creator)."/40.png'></div>
<div class='comments_cat'>
<div class='comments_top'>Comments</div>
<div class='comments'>".topicreplies($cid, $tid)."</div></div>
<div class='views_cat'>
<div class='views_top'>Views</div>
<div class='views'>".$views."</div></div>
</div></li>";
}
echo $topics;
} else {
echo "<div class='alert alert-danger text5'>There are no topics available yet.</div>";
}
?>
I have make the variable for you in the top.
Hope it work

if (mysql_num_rows($res) > 0) {
while ($row = mysql_fetch_assoc($res)) {
$tid = $row['id'];
$cid = $row['category_id'];
$title = $row['topic_title'];
$views = $row['topic_views'];
$date = $row['topic_date'];
$creator = $row['topic_creator'];
if ($row['topic_last_user']== "") { $last_user = getusername($row['topic_creator']); } else { $last_user = getusername($row['topic_last_user']); }
if ($row['topic_last_user']== "") { $last2_user = 'Started by'; } else { $last2_user = 'Most recent by'; }
$topics .= "<li class='category'><div class='left_cat'>
<a class='underline' href='view_topic.php?cid=".$cid."&tid=".$tid."'><div class='title'><i class='icon-comment'></i> ".$title."</div></a>
<div class='info'><i class='icon-comments icon-1'></i> ".topicreplies($cid, $tid)." Comments <i class='icon-user icon-1'>
</i>".$last2_user."&nbsp";
Syntax error is there at last line i.e ".$last2_user."
Just close the quote at "&nbsp";
This will solve your problem.

It looks like you are trying to insert an if statement inside the string you are creating. You can't do that as far as I know. However, the solution is easy. You end the string, insert your if statement, then append the rest of what you wanted. Like this:
if (mysql_num_rows($res) > 0) {
while ($row = mysql_fetch_assoc($res))
{
$tid = $row['id'];
$cid = $row['category_id'];
$title = $row['topic_title'];
$views = $row['topic_views'];
$date = $row['topic_date'];
$creator = $row['topic_creator'];
if ($row['topic_last_user']== "")
{ $last_user = getusername($row['topic_creator']); }
else
{ $last_user = getusername($row['topic_last_user']); }
if ($row['topic_last_user']== "")
{ $last2_user = 'Started by'; }
else
{ $last2_user = 'Most recent by'; }
$topics .= "<li class='category'><div class='left_cat'>
<a class='underline' href='view_topic.php?cid=".$cid."&tid=".$tid."'>
<div class='title'><i class='icon-comment'></i> ".$title."</div></a>
<div class='info'>
<i class='icon-comments icon-1'></i> ".topicreplies($cid, $tid)."
Comments <i class='icon-user icon-1'>
</i>".$last2_user."&nbsp";
// The string above is ended - and you insert your if statement.
// I changed the echo to an append to the string btw.
if ($info['rank'] == 1) { $topics .= "<div class='admin'>".$last_user."</div>"; } else { $topics .= "<div>".$last_user."</div>"; }
// Now you get to continue appending to the string.
$topics .= " <i class='icon-calendar'> </i> ".convertdate($date)."</div>
</div>";
$topics .= "<div class='right_cat'>
<div class='face'>
<img class='img_face' src='https://minotar.net/avatar/".getusername($creator)."/40.png'></div>
<div class='comments_cat'>
<div class='comments_top'>Comments</div>
<div class='comments'>".topicreplies($cid, $tid)."</div></div>
<div class='views_cat'>
<div class='views_top'>Views</div>
<div class='views'>".$views."</div></div>
</div></li>";
}
echo $topics;
} else {
echo "<div class='alert alert-danger text5'>There are no topics available yet.</div>";
}

The following code is what I have in the example provided above, and Here
<style type="text/css">
.user { font-weight:900; color: #000; text-decoration: none !important; }
.admin { font-weight:900; color: #F00; text-decoration: none !important; }
</style>
<form action="" method="POST">
Input number and press enter:
</br>User = 0 Admin = 1</br>
<input name="rank" id="rank" type="text" />
</form>
<?php
if (isset($_POST['rank'])) {
$info['rank'] = $_POST['rank'];
if($info['rank'] == 1) { $x= "<a class='admin'>Admin</a>"; }
else { $x= "<a class='user'>User</a>"; }
echo $x;
}
?>
As I mentioned in my comment above your code works fine, unless there is something going on elsewhere that we cant see with what you have given us.
And without a form.
<style type="text/css">
.user { font-weight:900; color: #000; text-decoration: none !important; }
.admin { font-weight:900; color: #F00; text-decoration: none !important; }
</style>
<?php
$info['rank'] = 1;
if($info['rank'] == 1) { $x= "<a class='admin'>Admin</a>"; }
else { $x= "<a class='user'>User</a>"; }
echo $x;
?>

Related

Query using data from wrong column

The title may be a bit misleading as I didn't know exactly how to phrase it. I have a PHP page which shows a listing of records with search filters. The code is given below. The page gets displayed correctly but in one of the columns I get following database error for MS SQL
Error Number: 22018
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Conversion failed when converting the varchar value 'Marie Stopes International' to data type int.
SELECT * FROM ctbl_mas_funder WHERE flag = 0 AND id = 'Marie Stopes International'
Filename: C:\inetpub\wwwroot\FMS\system\database\DB_driver.php
Line Number: 330
I think its because instead of pulling an ID for query it is pulling the data from some other column, but cannot quite put my finger on what is causing this.
The code for the page is as below.
<link href="<?php echo base_url(); ?>assets/js/plugins/forms/select2/select2.css" rel="stylesheet" />
<script src="<?php echo base_url(); ?>assets/js/plugins/forms/uniform/jquery.uniform.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/plugins/forms/select2/select2.js"></script>
<script src="<?php echo base_url(); ?>assets/js/plugins/tables/datatables/jquery.dataTables.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/plugins/tables/datatables/jquery.dataTables.columnFilter.js"></script>
<script src="<?php echo base_url(); ?>assets/js/app.js"></script><!-- Core js functions -->
<script src="<?php echo base_url(); ?>assets/js/pages/data-tables.js"></script><!-- Init plugins only for page -->
<div class="wrapper">
<div class="crumb">
<ul class="breadcrumb">
<li><i class="icon16 i-home-4"></i>Home</li>
<li>Manage Funding</li>
<li class="active">
<?=$title ?>
</li>
</ul>
</div>
<div class="container-fluid">
<div id="heading" class="page-header">
<h1><i class="icon20 i-table-2"></i>
<?=$title ?>
</h1>
</div>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
<div class="icon"><i class="icon20 i-table"></i></div>
<h4>
<?=$title ?>
</h4>
<div class="flexigrid" style="width: 100%;">
<div class="tDiv">
<div class="tDiv2">
<?php
$f_manange_funding=explode(',', $this->session->userdata('f_manange_funding'));
if (in_array('6', $m_projects) or $this->session->userdata('user_type')=='admin')
{?>
<!--<div class="fbutton tip" title="Download Word" onclick="javascript:document.location.href='<?php echo site_url("office").'/'.$this->uri->segment(2).'/'.$this->uri->segment(3); ?>/download_word'"><div><span class="word" style="padding-left: 20px;">Word</span></div></div>
<div class="btnseparator"></div>-->
<?php }
if (in_array('5', $m_projects) or $this->session->userdata('user_type')=='admin')
{?>
<!--<div class="fbutton tip" title="Download Excel" onclick="javascript:document.location.href='<?php echo site_url("office").'/'.$this->uri->segment(2).'/'.$this->uri->segment(3); ?>/download_excel'"><div><span class="excel" style="padding-left: 20px;">Excel</span></div></div> <div class="btnseparator"></div> -->
<?php }
if (in_array('7', $m_projects) or $this->session->userdata('user_type')=='admin')
{?>
<!--<div class="fbutton tip" title="Download Pdf" onclick="javascript:document.location.href='<?php echo site_url("office").'/'.$this->uri->segment(2).'/'.$this->uri->segment(3); ?>/download_pdf'"><div><span class="pdf" style="padding-left: 20px;">Pdf</span></div></div>
<div class="btnseparator"></div>-->
<?php }
if (in_array('8', $m_projects) or $this->session->userdata('user_type')=='admin')
{?>
<!--<div class="fbutton tip" title="Print" onclick="javascript:window.open('<?php echo site_url("office").'/'.$this->uri->segment(2).'/'.$this->uri->segment(3); ?>/print_page', 'mywindow', 'location=1,status=1,scrollbars=1, width=600,height=600')"><div><span class="print" style="padding-left: 20px;">Print</span></div></div>
<div class="btnseparator"></div>-->
<?php } ?>
<div class="fbutton tip" title="Help" >
<div><span class="help_ic" style="padding-left: 20px;">Help</span></div>
</div>
</div>
<div style="clear:both"></div>
</div>
</div>
</div>
<!-- End .panel-heading -->
<div class="panel-body">
<?php
//flash messages
if($this->session->flashdata('feedback'))
{
echo '<div class="alert alert-success">';
echo '<a class="close" data-dismiss="alert">×</a>';
echo $this->session->flashdata('feedback');
echo '</div>';
} ?>
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered table-hover" id="dataTable">
<thead>
<tr>
<th > Funding Hierarchy</th>
<th >Project Code </th>
<th >Country of implementation</th>
<th >Source of funding </th>
<th > Primary Donor</th>
<th > Contracting donor</th>
<th > Contracting recipient </th>
<th >Short Title</th>
<th >Funding status</th>
<th width="8%">Action</th>
</tr>
</thead>
<tbody>
<?php
$str="";
if(($funding_mechanism !="") or ($primary_donor !="") or ($contracting_donor_funder_code !="" ) or ($funder_code_primary !="" ) or ($funding_manager !="" ) ){
if($fundingstatus !="")
{
$str=$str."ctbl_contract_funding_info.fundingstatus ='$fundingstatus'";
}
if($project_code !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.project_code='$project_code'";
}
if($fundingHierarchy !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.fundingHierarchy='$fundingHierarchy'";
}
if($funding_mechanism !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_fin.funding_mechanism='$funding_mechanism'";
}
if($subfunding_title !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.subfunding_title='$subfunding_title'";
}
if($primary_donor !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.primary_donor='$primary_donor'";
}
if($source_funding !="")
{
if($str!="")
{
$str=$str." and ";
}
// $str=$str."ctbl_contract_funding_info.source_funding='$source_funding'";
$str=$str."ctbl_contract_funding_info.source_funding REGEXP '.*;s:[0-9]+:\"$source_funding\".*'";
}
if($contracting_donor !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.contracting_donor='$contracting_donor'";
}
if($contracting_recipient !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.contracting_recipient='$contracting_recipient'";
}
if($funder_code_source !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.funder_code_source='$funder_code_source'";
}
if($contracting_donor_funder_code !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.contracting_donor_funder_code='$contracting_donor_funder_code'";
}
if($funder_code_primary !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.funder_code_primary='$funder_code_primary'";
}
if($country_implemention !="")
{
if($str!="")
{
$str=$str." and ";
}
// $str=$str."ctbl_contract_funding_info.country_implemention='$country_implemention'";
$str=$str."ctbl_contract_funding_info.country_implemention REGEXP '.*;s:[0-9]+:\"$country_implemention\".*'";
}
if($funding_manager !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_mgmt.funding_manager='$funding_manager'";
}
$query_search = $this->db->query('SELECT ctbl_contract_funding_info.* ,ctbl_contract_funding_mgmt.funding_manager,ctbl_contract_workflow.id as id1,ctbl_contract_workflow.family_id as main_id ,ctbl_contract_workflow.parent_type
FROM ctbl_contract_workflow
LEFT JOIN ctbl_contract_funding_info ON ctbl_contract_funding_info.contract_id = ctbl_contract_workflow.id
LEFT JOIN ctbl_contract_funding_mgmt ON ctbl_contract_funding_mgmt.contract_id = ctbl_contract_funding_info.contract_id
LEFT JOIN ctbl_contract_funding_fin ON ctbl_contract_funding_fin.contract_id = ctbl_contract_funding_mgmt.contract_id
WHERE '.$str.'
ORDER BY ctbl_contract_workflow.id asc');
$total=$query_search->num_rows() ;
foreach($query_search->result_array() as $row_search)
{
echo '<tr>';
echo '<td>'.$row_search['fundingHierarchy'].'</td>';
if($row_search['project_code']!='')
{
echo '<td>E'.$row_search['project_code'].'</td>';
}
else
{
echo '<td></td>';
}
if($row_search['country_implemention']=='')
{
$data['list1'] = $this->common_model->get_by_id('id',$country_implemention,'ctbl_mas_country');
echo '<td>'.$data['list1'][0]['name'].'</td>';
}
else
{
?>
<td><?php $query = $this->db->query('SELECT * FROM ctbl_mas_country order by rank desc');
$total=$query->num_rows() ;
if($total>0)
{
foreach ($query->result_array() as $row)
{
if(unserialize($row_search['country_implemention'])!='')
{
$implementing_agency= unserialize($row_search['country_implemention']);
}
else
{
$implementing_agency=array();
}
if (in_array($row['id'], $implementing_agency))
{
echo $row['name']."<br>";
}
}
}
?></td>
<?php
}
if($source_funding!='' || $source_funding!='Array')
{
$data['list'] = $this->common_model->get_by_id('id',$source_funding,'ctbl_mas_funder');
echo '<td>'.$data['list'][0]['name'].'</td>';
}
else
{
?>
<td><?php
$query = $this->db->query('SELECT * FROM ctbl_mas_funder Where flag=0 ');
$total=$query->num_rows() ;
if($total>0)
{
foreach ($query->result_array() as $row)
{
if(unserialize($row_search['source_funding'])!='')
{
$source_funding= unserialize($row_search['source_funding']);
}
else
{
$source_funding=array();
}
if (in_array($row['id']."-".$row['code'], $source_funding))
{
echo $row['name']."<br>";
}
}
}
?></td>
<?php
}
$data['list1'] = $this->common_model->get_by_id('id',substr($row_search['primary_donor'],0, strpos($row_search['primary_donor'], "-")),'ctbl_mas_funder');
echo '<td>'.$data['list1'][0]['name'].'</td>';
?>
<td><?php
$c=explode("-",$row_search['contracting_donor']);
if(is_array($c) && $c[1]!='')
{
$data['list2'] = $this->common_model->get_by_id('id',$c[0],'ctbl_mas_funder');
echo $data['list2'][0]['name'];
}
else
{
echo $row_search['contracting_donor'];
}
?></td>
<td><?php
if($row_search['contracting_recipient']!='Select')
{
if($row_search['type_recipient']!='Select')
{
if($row_search['type_recipient']=='MSI partnership')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_msi_entities');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
else if($row_search['type_recipient']=='External partner')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_external_partners');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
else if($row_search['type_recipient']=='MSI funding stream')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_msi_funding_streams');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
else if($row_search['type_recipient']=='MSI funding stream - project donor')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_funder');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
}}?></td>
<?php
echo '<td>'.$row_search['subfunding_title'].'</td>';
echo '<td>'.$row_search['fundingstatus'].'</td>';
echo '<td class="crud-actions">';
if($row_search['main_id']==$row_search['id1'])
{
echo '<i class="icon16 i-zoom-in"></i> ';
}
else
{
echo '<i class="icon16 i-zoom-in"></i> ';
}
//echo '<i class="icon16 i-pencil"></i> ';
//echo '<a class="tip" title="Delete" href="'.site_url("office").'/master/create_entry/delete/?contract_id='.$this->encryption->encode($row_search['id']).'" title="Remove"><i class="icon16 i-remove"></i></a>';
echo ' </td></tr>';
}
/// to show no records
/// to show no records
}else{
//// searching results query ends here
//// searching results query echo ends here
$query_search = $this->db->query('SELECT ctbl_contract_funding_info.* ,ctbl_contract_funding_mgmt.funding_manager,ctbl_contract_workflow.id as id1,ctbl_contract_workflow.family_id as main_id,ctbl_contract_workflow.parent_type
FROM ctbl_contract_workflow
LEFT JOIN ctbl_contract_funding_info ON ctbl_contract_funding_info.contract_id = ctbl_contract_workflow.id
LEFT JOIN ctbl_contract_funding_mgmt ON ctbl_contract_funding_mgmt.contract_id = ctbl_contract_workflow.id
LEFT JOIN ctbl_contract_funding_fin ON ctbl_contract_funding_fin.contract_id = ctbl_contract_funding_mgmt.contract_id
ORDER BY ctbl_contract_workflow.id asc');
$total=$query_search->num_rows() ;
foreach($query_search->result_array() as $row_search)
{
echo '<tr>';
echo '<td>'.$row_search['fundingHierarchy'].'</td>';
if($row_search['project_code']!='')
{
echo '<td>E'.$row_search['project_code'].'</td>';
}
else
{
echo '<td>'.$row_search['project_code'].'</td>';
}
?>
<td><?php $query = $this->db->query('SELECT * FROM ctbl_mas_country order by rank desc');
$total=$query->num_rows() ;
if($total>0)
{
foreach ($query->result_array() as $row)
{
if(unserialize($row_search['country_implemention'])!='')
{
$implementing_agency= unserialize($row_search['country_implemention']);
}
else
{
$implementing_agency=array();
}
if (in_array($row['id'], $implementing_agency))
{
echo $row['name']."<br>";
}
}
}
?></td>
<td><?php
$query = $this->db->query('SELECT * FROM ctbl_mas_funder Where flag=0 ');
$total=$query->num_rows() ;
if($total>0)
{
foreach ($query->result_array() as $row)
{
if(unserialize($row_search['source_funding'])!='')
{
$source_funding= unserialize($row_search['source_funding']);
}
else
{
$source_funding=array();
}
if (in_array($row['id']."-".$row['code'], $source_funding))
{
echo $row['name']."<br>";
}
}
}
?></td>
<?php
$data['list1'] = $this->common_model->get_by_id('id',substr($row_search['primary_donor'],0, strpos($row_search['primary_donor'], "-")),'ctbl_mas_funder');
echo '<td>'.$data['list1'][0]['name'].'</td>';
?>
<td>
<?php
//echo $row['contracting_donor'];
$code_id =explode("-",$row_search['contracting_donor']);
if(trim($row_search['fundingHierarchy']) == 'Donor Funding'){
$data['donor_name'] = $this->common_model->get_by_id('id',$code_id[0],'ctbl_mas_funder');
echo $data['donor_name'][0]['name'];
}
else{
echo $row_search['contracting_donor'];
}
?>
</td>
<td>
<?php
if($row_search['contracting_recipient']!='Select')
{
if($row_search['type_recipient']!='Select')
{
if($row_search['type_recipient']=='MSI partnership')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_partnership');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
else if($row_search['type_recipient']=='External partner')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_external_partners');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
else if($row_search['type_recipient']=='MSI funding stream')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_msi_funding_streams');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
else if($row_search['type_recipient']=='MSI funding stream - project donor')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_funder');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
}
}
?>
</td>
<?php
echo '<td>'.$row_search['subfunding_title'].'</td>';
echo '<td>'.$row_search['fundingstatus'].'</td>';
echo '<td class="crud-actions">';
if($row_search['main_id']==$row_search['id1'])
{
echo '<i class="icon16 i-zoom-in"></i> ';
}
else
{
echo '<i class="icon16 i-zoom-in"></i> ';
}
//echo '<i class="icon16 i-pencil"></i> ';
//echo '<a class="tip" title="Delete" href="'.site_url("office").'/master/create_entry/delete/?contract_id='.$this->encryption->encode($row_search['id']).'" title="Remove"><i class="icon16 i-remove"></i></a>';
echo ' </td></tr>';
}
}
?>
</tbody>
</table>
<?php require_once('help.php');?>
<div class="form-group">
<div class="col-lg-offset-4">
<div class="pad-left15"> <i class="i-cancel"></i>Cancel/Back </div>
</div>
</div>
</div>
<!-- End .panel-body -->
</div>
<!-- End .widget -->
</div>
<!-- End .col-lg-12 -->
</div>
<!-- End .row-fluid -->
</div>
<!-- End .container-fluid -->
</div>

Fetching replace text with image, do not working HTML Prob

This is my code:
echo "<div class=\"panel-body\" style=\"border-top: 1px solid; border-bottom: 1px solid; border-color:#ddd\">";
echo "<div class=\"vidMR\">";
echo"<div><strong>" . $lang['vldMaker'] . "</strong></div>";
echo"<div>";
$maker = "SELECT vidMaker FROM videoinformation WHERE id=".$row['id'];
if ($result1 = mysqli_query($con, $maker)) {
while ($row2 = mysqli_fetch_row($result1)) {
$values = explode(',',$row2[0]);
foreach($values as $v1)
if (!empty($v1)) {
printf ("<img src=\"addVid/maker/%s.jpg\" class=\"makerImg img-circle\" alt=\"%s.jpg\">", $v1, $v1);
}
}
mysqli_free_result($result1);
}
else{
echo $lang['vldErrorMaRo'];
}
echo"</div>";
echo"<div><strong>" . $lang['vldRoles'] . "</strong></div>";
echo"<div>";
$role = "SELECT vidRoles FROM videoinformation WHERE id=".$row['id'];
if ($result2 = mysqli_query($con, $role)) {
while ($row3 = mysqli_fetch_row($result2)) {
$values = explode(',',$row3[0]);
foreach($values as $v2)
if (!empty($v2)) {
printf ("<img src=\"addVid/roles/%s.jpg\" class=\"roleImg img-circle\" alt=\"%s.jpg\">", $v2, $v2);
}
}
mysqli_free_result($result2);
}
else{
echo $lang['vldErrorMaRo'];
}
echo "</div>";
echo"</div></div>";
Problem is that this code working ONLY in chrome and opera, BUT in IE and Mozilla pictures are not displayed...
I tried different ways...like add full path for images and so on... BUT any way it's would not like to work in IE and Mozilla.
This is out put HTML...
<body>
<div class="vidMR">
<div>
<strong>Maker:</strong>
</div>
<div><img alt="Name1.jpg" class="makerImg img-circle" src=
"addVid/maker/Name1.jpg"><img alt="Name2.jpg" class=
"makerImg img-circle" src="addVid/maker/Name2.jpg"></div>
<div>
<strong>Roles:</strong>
</div>
<div><img alt="Name1.jpg" class="roleImg img-circle" src=
"addVid/roles/Name1.jpg"><img alt="Name2.jpg" class=
"roleImg img-circle" src="addVid/roles/Name2.jpg"><img alt="Name3.jpg"
class="roleImg img-circle" src="addVid/roles/Name3.jpg"></div>
</div>
</body>
</html>

Fetch data from mysql to textarea, but if not exist print null

I made some little translator.
And if word exist in database all is ok. But if word didnt exist in database, word isnt printed. I add some code to print if result is empty but didnt work.
Any idea why word that not exist in database isnt printed?
Here is code:
<p><form method="post" action="prevedi.php">
<textarea id="prevedi" name="prevedi" style="margin: 2px; height: 137px; width: 380px;">
<?php
echo htmlentities($_POST['prevedi']);
?>
</textarea>
<textarea id="prevod" disabled="disabled" name="prevod" style="margin: 2px; height: 137px; width: 380px; border: 0px;" readonly>
<?
if (isset($_POST['prevedi'])) {
//Kreci
$prevedi = htmlentities($_POST['prevedi']);
$prevedi = explode(" ",$prevedi);
foreach ($prevedi as $word) {
$slovo = $word[0];
$result = mysqli_query($con,"SELECT * FROM $slovo WHERE srpski='$word'");
if (!empty($result)) {
while($row = mysqli_fetch_array($result))
{
$prevod .= $row['romski']." ";
}
}
else
{
$prevod .= "".$word." ";
}
}
echo $prevod;
//Kraj isset
}
?>
</textarea><br>
<input name="translate" type="submit" value="Translate"/>
</form>
</p>
Because your result is not empty. You should check if $result number of rows is > 0
if ($result->num_rows > 0) {
while($row = mysqli_fetch_array($result)){
$prevod .= $row['romski']." ";
}
}else{
$prevod .= $word." ";
}
i think you have to initialize $prevod before assigning it the way you did.
$prevod = "";
if (!empty($result)) {
while($row = mysqli_fetch_array($result))
{
$prevod .= $row['romski']." ";
}`enter code here`
}
else
{
$prevod .= "".$word." ";
}
}
echo $prevod;

Images in loop with different div class in PHP

Suppose I have code like bellow
<?php
$sql = "SELECT * FROM images";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)) {
echo '<div class="single">';
echo '<img src="'.$row['image'].'" alt="'.$row['title'].' " />';
echo '</div>';
}
?>
And I want to append first and last in div class <div class="single"> in every set of 5 images i.e
<div class="single first">
<img src="image1.jpg" alt="title1" />
</div>
<div class="single">
<img src="image2.jpg" alt="title2" />
</div>
<div class="single">
<img src="image3.jpg" alt="title3" />
</div>
<div class="single">
<img src="image4.jpg" alt="title4" />
</div>
<div class="single last">
<img src="image5.jpg" alt="title5" />
</div>
How to do it through loop, please help,
Thanks :)
Using modulo:
<?php
$sql = "SELECT * FROM images";
$query = mysql_query($sql);
$i = 0;
$class = "";
while($row = mysql_fetch_assoc($query)) {
if($i % 5 == 0){
$class = 'first';
}else if($i % 5 == 4){
$class = 'last';
}else{
$class = "";
}
echo '<div class="single ' . $class . '">';
echo '<img src="'.$row['image'].'" alt="'.$row['title'].' " />';
echo '</div>';
$i++;
}
?>
<?php
$sql = "SELECT * FROM images";
$query = mysql_query($sql);
$x=0;
while($row = mysql_fetch_assoc($query)) {
$x++;
if ( $x == 1 ) { $c = ' first'; }
elseif ( $x == 5 ) { $c = ' last'; }
else { $c = ''; }
echo '<div class="single' . $c . '">';
echo '<img src="'.$row['image'].'" alt="'.$row['title'].' " />';
echo '</div>';
}
?>
You need to count your iterations, and use the modulo function to get parts of 5:
$sql = "SELECT * FROM images";
$query = mysql_query($sql);
$i = 0;
while($row = mysql_fetch_assoc($query)) {
if($i % 5 == 0)
echo '<div class="single first">';
else if($i % 5 == 4)
echo '<div class="single last">';
else
echo '<div class="single">';
echo '<img src="'.$row['image'].'" alt="'.$row['title'].' " />';
echo '</div>';
$i++;
}
How about this:
$index = 0;
while($row = mysql_fetch_assoc($query)) {
$classes = 'single';
switch ($index % 5) {
case 0:
$classes .= ' first';
break;
case 4:
$classes .= ' last';
break;
}
echo <<<HTML
<div class="$classes">
<img src="{$row['image']}" alt="{$row['title']}" />
</div>;
HTML;
$index++;
}
As a sidenote, have you considered using CSS nth-child property instead?

SQL/PHP query works in PHPmyAdmin but not the site

SQL/PHP query works in PHPmyAdmin but not the site.
I notice that many have had this problem but admittedly I am not as advanced as some of the coders on this site...yet. =) I humbly request any experience you may have laying around :P Thank you.
<?php
// session_start();
// ob_start();
ini_set("display_errors", true);
error_reporting(-1);
// Connection to database.
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('') or die(mysql_error());
?>
<?php
// Maintenance page.
$maintenance = 1; // 1 = Site Online || 0 = Site Offline
if ($maintenance == 0) {
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<style type="text/css">
body {
color:#ffffff;
background-color: #000000;
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<title></title>
</head>
<body>
<center><img src="images/p4flogo.png" /></center><br />
<?php
echo "<br/><br /><center>This site is currently under maintenance.</center>";
} else {
// Start of main website.
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<style type="text/css">
body {
color:#ffffff;
background-color: #000000;
font-family: Arial, Helvetica, sans-serif;
}
a:link {color: orange; text-decoration: underline; }
a:active {color: red; text-decoration: underline; }
a:visited {color: orange; text-decoration: underline; }
a:hover {color: red; text-decoration: none; }
table {
border-color: #333333;
}
th {
background-color:#ffffff;
color:#000000;
font-family: Arial, Helvetica, sans-serif;
height:30px;
}
td { font-family: Arial, Helvetica, sans-serif;
color:#ffffff;
height:35px;
}
</style>
</head>
<title></title>
</head>
<body>
<table border="0" cellspacing="1" align="center">
<tr><td>
<center><img src="images/p4flogo.png" /></center><br /><br />
<form action="" method="post">
Search for a soldier: <input type="text" name="value" size="35" /><input type="submit" value="search" /><br />
<?php
if (isset($_POST['value']) && !empty($_POST['value'])) {
$value = mysql_real_escape_string($_POST['value']);
// query to database for soldier stats
// query works in phpmyadmin but not on site.
$sql = "
SELECT
`Name`,
MAX(`Level`),
`Class`,
SUM(`Kills`),
SUM(`Deaths`),
SUM(`Points`),
SUM(`TotalTime`),
SUM(`TotalVisits`),
`CharacterID`
FROM
`Characters`
WHERE
`Name` LIKE '$value%' OR `CharacterID` LIKE '$value'
GROUP BY
`Name`,
`Class`,
`CharacterID`
ORDER BY
`Name` ASC;";
$query = mysql_query($sql);
$numrow = mysql_num_rows($query);
if ($numrow >= 1) {
echo "<br /><b>View TOP 100 Players!</b><br />";
echo "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\"> ";
echo "<th>Soldier Name</th><th>Level</th><th>Class</th><th>KDR</th><th>Kills</th><th>Deaths</th><th>Points</th><th>Hours Played</th><th>Total Visits</th><th>CharacterID</th>";
echo "<br />";
WHILE ($row = mysql_fetch_assoc($query)) {
$SoldierName = $row['Name'];
$Level = $row['Level'];
$Class = $row['Class'];
if ($Class == NULL | empty($Class)) {
$Class = '<center>n / a</center>';
}
if ($Class == 1) {
$Class = 'Assault';
}
if ($Class == 2) {
$Class = 'Recon';
}
if ($Class == 3) {
$Class = 'Medic';
}
if ($Class == 4) {
$Class = 'Engineer';
}
echo $Kills = $row['Kills'];
if ($Kills == 0) {
$Kills = 1;
}
$Deaths = $row['Deaths'];
if ($Deaths == 0) {
$Deaths = 1;
}
$Kdr = round($Kills / $Deaths, 2);
$Points = $row['Points'];
$TimePlayed = $row['TotalTime'];
if ($TimePlayed == 0) {
$TimePlayed = 1;
} else {
$TimePlayed = round(($TimePlayed / 3600), 0);
}
$TotalVisits = $row['TotalVisits'];
$CharacterID = $row['CharacterID'];
echo "<tr>";
echo "<td><b>$SoldierName</b></td>";
echo "<td>$Level</td>";
echo "<td>$Class</td>";
if ($Kdr > 3.9) {
echo "<td><font color=\"red\"><b>$Kdr</b></font></td>";
} else if ($Kdr > 2.5 && $Kdr < 4) {
echo "<td><font color=\"orange\"><b>$Kdr</b></font></td>";
} else {
echo "<td><font color=\"limegreen\">$Kdr</font></td>";
}
echo "<td>$Kills</td>";
echo "<td>$Deaths</td>";
echo "<td>$Points</td>";
echo "<td>$TimePlayed</td>";
echo "<td>$TotalVisits</td>";
echo "<td>$CharacterID</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No player found with that name. Please try again.";
}
} else {
if (empty($_POST['value'])) {
echo "<font color=\"red\">You must enter a search value.</font>";
}
// query to p4f database for top 100 players.
$sql = "SELECT * FROM `Characters` WHERE `Points` > 1 GROUP BY `Name` ORDER BY `Points` DESC LIMIT 100;";
$query = mysql_query($sql);
echo "<h3>TOP 100 PLAYERS</h3>";
echo "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\"> ";
echo "<th></th><th>Soldier Name</th><th>Level</th><th>Class</th><th>KDR</th><th>Kills</th><th>Deaths</th><th>Points</th><th>Hours Played</th><th>Total Visits</th><th>CharacterID</th>";
// echo "Made it to loop!";
$Rank = 1;
WHILE ($row = mysql_fetch_assoc($query)) {
$SoldierName = $row['Name'];
$Level = $row['Level'];
$Class = $row['Class'];
if ($Class == NULL | empty($Class)) {
$Class = '<center>n / a</center>';
}
if ($Class == 1) {
$Class = 'Assault';
}
if ($Class == 2) {
$Class = 'Recon';
}
if ($Class == 3) {
$Class = 'Medic';
}
if ($Class == 4) {
$Class = 'Engineer';
}
$Kills = $row['Kills'];
if ($Kills == 0) {
$Kills = 1;
}
$Deaths = $row['Deaths'];
if ($Deaths == 0) {
$Deaths = 1;
}
$Kdr = round($Kills / $Deaths, 2);
$Points = $row['Points'];
$TimePlayed = $row['TotalTime'];
if ($TimePlayed == 0) {
$TimePlayed = 1;
} else {
$TimePlayed = round(($TimePlayed / 3600), 0);
}
$TotalVisits = $row['TotalVisits'];
$CharacterID = $row['CharacterID'];
echo "<tr>";
echo "<td>$Rank</td>";
echo "<td><b>$SoldierName</b></td>";
echo "<td>$Level</td>";
echo "<td>$Class</td>";
if ($Kdr > 3.9) {
echo "<td><font color=\"red\"><b>$Kdr</b></font></td>";
} else if ($Kdr > 2.5 && $Kdr < 4) {
echo "<td><font color=\"orange\"><b>$Kdr</b></font></td>";
} else {
echo "<td><font color=\"limegreen\">$Kdr</font></td>";
}
echo "<td>$Kills</td>";
echo "<td>$Deaths</td>";
echo "<td>$Points</td>";
echo "<td>$TimePlayed</td>";
echo "<td>$TotalVisits</td>";
echo "<td>$CharacterID</td>";
echo "</tr>";
$Rank++;
}
echo "</table>";
}
}
?>
</td></tr>
</table>
</body>
</html>
I'm not sure about MySQL, but I know that in SQL when you do an aggregate function, like SUM(Kills), then you can't reference the row via $row['kills']. I don't know if this is your problem, but you could try doing SUM(Kills) as 'kills' in your SELECT statement. Doing this for your aggregate SELECTs will allow you to reference them all this way.

Categories