PHP Display option depending on value - php

I am editing a part of a script that displays gender of a person but trying to add additional options.
The form that saves the information saves it as a value.
In my lang file this is what i have got related to the part i am trying to change :
$LNG['powner'] = 'Pet Owner';
$LNG['dog'] = 'Dog';
$LNG['cat'] = 'Cat';
$LNG['rabbit'] = 'rabbit';
The part of the code im trying to display is
((!empty($profile['gender'])) ? '<div class="sidebar-list">'.$LNG['ttl_gender'].':
<strong>'.(($profile['gender'] == 1) ? $LNG['powner'] :($profile['gender'] == 2) ?
$LNG['cat'] : ($profile['gender'] == 3) ? $LNG['rabbit'] : $LNG['rabbit']).'</strong>
</div>' : '').'
But it will only display cat or rabbit ...
I am thinking that the code is set to be one or the other because this works :
((!empty($profile['gender'])) ? '<div class="sidebar-list">'.$LNG['ttl_gender'].'
: <strong>'.(($profile['gender'] == 1) ? $LNG['dog'] : $LNG['cat']).'</strong></div>'
: '').
after the code if follows
'.((!empty($profile['website'])) ? '<div class="sidebar-list">'
.$LNG['profile_website'].': <strong><a href="'.$profile['website'].
'" target="_blank" rel="nofllow">'.$LNG['profile_view_site'].'</a>
</strong></div>' : '').'

Your code could be re-written to say:
if(!empty($profile['gender']) {
echo '<div class="sidebar-list">'.$LNG['ttl_gender'];
} else {
echo "<strong>";
if($profile['gender'] == 1) {
echo $LNG['powner'];
} else {
if($profile['gender'] == 2) {
echo $LNG['cat'];
}
else {
if($profile['gender'] == 3){
echo $LNG['rabbit'];
} else {
echo $LNG['rabbit'];
}
}
}
}
As you see the code using shorthand If/Else statements is confusing to read, and can lead to mistakes. Re-write your functionality in more readable way, and you will prevent errors, and confusion with regards to what the actual conditions should be.
I'm assuming that the problem you're having is that the users you're testing this on are either gender 2 or 3, and never 1.

Related

Changing links by priority - PHP / HTML

I am currently working on a school project and appreciate your help.
HTML Code:
LINK 1
LINK 2
LINK 3
Now as I am relatively new to PHP, I need clue wehere to start or which topics are relevant for my solution. I have already searched the Forum here and with google, but unforutenly, didn't find anything, that fits my needs.
What I need, is a PHP-Code, that contains 6 href Links and every Link has a priority. So every time somebody clicks on "LINK 1" on the html file, he gets directed to the PHP-File "change-1.php", where one of the six href Links get activated.
PHP CODE "change-1.php":
<?php
if (condition) {
echo "http://www.someotherwebsite-1.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-2.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-3.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-4.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-5.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-6.com";
}
?>
Perhaps this should help
LINK 1
LINK 2
LINK 3
Just create one change.php file
//to get change
<?php
if ($_GET['change'] == 1) {
echo "http://www.someotherwebsite-1.com";
}
elseif ($_GET['change'] == 2) {
echo "http://www.someotherwebsite-2.com";
}
elseif ($_GET['change'] == 3) {
echo "http://www.someotherwebsite-3.com";
}
elseif ($_GET['change'] == 4) {
echo "http://www.someotherwebsite-4.com";
}
elseif ($_GET['change'] == 5) {
echo "http://www.someotherwebsite-5.com";
}
elseif ($_GET['change'] == 6) {
echo "http://www.someotherwebsite-6.com";
}
?>
Now this is the basic idea. You can manipulate the data however you want.
I hope you got the concept.

If statement not working?

I am a beginner when it comes to php.... I have the following:
I am not sure if it is my if statement expressed wrong or just at the wrong place. I want it not to show the records when the companyname == the businessname! Please advise?
if (mysql_num_rows($sql) > 0 && **($row['companyname'] == $user_data['businessname'])** )
{
while ($row = mysql_fetch_array($sql)){
if (($employed =='1')){
echo '<h4> ID : '.$row['idnumber'] ;
echo '<br> First Name : '.$row['firstname'];
echo '<br> Last Name : '.$row['lastname'];
echo '<br> Reference 1 : '.$row['ref1'];
echo '<br> Reference 2 : '.$row['ref2'];
echo '<br> Reference 3 : '.$row['ref3'];
echo '<br> Gender : '.$row['gender'];
echo '<br> Company : '.$row['companyname'];
echo ' </h4>';
echo '<br />';
echo '<h2>Some Additional Options</h2>';
echo '<br />';
include 'includes/admenu.php';
}
}
}
else
{
print ("$XX");
}
The $row variable doesn't exist outside your loop, so it's pointless trying to use it in the if statement. You need to move it inside the loop, like so:
while ($row = mysql_fetch_array($sql))
{
if ( ($employed =='1') && ($row['companyname'] == $user_data['businessname']) )
{
// code ...
}
}
I want it not to show the records when the companyname == the businessname!Move the statement inside your loop:
I'm not actually sure if you're trying to display when companyname == the businessname or companyname != the businessname. Either way, change your if condition accordingly and it should work.

How to make current page with different style (PHP ?page href)?

My piece of code.
And problem now are about styling, i just want style page number, wich is open, like a active id, if page open, then that number is in different color or smth else.
I need make a new variable? Or what, i just try to add but its wont work, it only works if at the end i write echo $page; , then it show style on this, but i need on links, numbers.
<?php
if($total_pages > 1){
if($page != 1){
echo ' < ';
}
for($number=1;$number<=$total_pages;$number++)
{
echo ''.$number.'';
}
if($page != $total_pages){
echo ' > ';
}
}
?>
You can do something like this.
$currentPageNumber = $_GET['page'];
for($number=1;$number<=$total_pages;$number++){
$currentPageStyle = '';
if($number == $currentPageNumber){
$currentPageStyle = 'style="color:red"';
}
echo '<a href="?page='.$number.'" '.$currentPageStyle.'>'.$number.'</a>';
}

Problems with "onload" and XHTML Validation

My name is Manuel, I am a web design student and am starting to take my first steps with web design. Recently I tried to validate
this site:
http://accesosnormalizados.com
I used the W3C validator, at first I found about 30 errors, and can correct them all except one that says: 'there is no attribute
"onload"'.
Apparently not support XHTML onload tag, and I use a Joomla extension called Vertical Menu using onload. This is a free extension and works well but I have found it has some bugs, especially when validating the website.
This is the PHP code for extension:
get( 'menutype', 'mainmenu' );
$qry = "SELECT id, name,parent, link,type,browserNav FROM #__menu WHERE menutype = '".$menutype."' AND published = 1 ORDER BY ordering";
$database->setQuery($qry);
$rows = $database->loadObjectList();
if(isset($GLOBALS['vertical_menu'])) $GLOBALS['vertical_menu']++;
else $GLOBALS['vertical_menu'] = 0;
function getMenuChildList($rows, $parentId) {
$childRows = array();
foreach ($rows as $row) {
if ($row->parent == $parentId) {
$childRows[] = $row;
}
}
return $childRows;
}
function drawVerticalMenu($rows, $showsubcats, $parentId = 0) {
$categories = $showsubcats || !$parentId ? getMenuChildList($rows, $parentId) : array();
if ($parentId) {
if (!count($categories)) {
echo '';
return;
} else echo '';
echo '';
} else echo '';
echo '';
foreach ($categories as $category) {
$link = $category->link. (preg_match("/^http:\/\/|^https:\/\//",$category->link)? "" : '&Itemid='.$category->id);
$blank = $category->browserNav? ' target="_blank" ' : ' ';
echo 'id.'" class="menu">'.$category->name.' ';
drawVerticalMenu($rows, $showsubcats, $category->id);
}
echo '';
if ($parentId && count($categories)) echo '';
}
$document = &JFactory::getDocument();
$document->addScript('https://ajax.googleapis.com/ajax/libs/dojo/1.5.0/dojo/dojo.xd.js');
$document->addScript('modules/mod_vertical_menu/script/menu.js');
$document->addStyleSheet('modules/mod_vertical_menu/style/menu.css');
$document->addCustomTag('
div#MenuContainer'.$GLOBALS['vertical_menu'].' table#VerticalMenu'.$GLOBALS['vertical_menu'].' {
width: '.$params->get('categorymenu_width', 150).'px;
opacity: '.$params->get('categorymenu_out', 0.8).';
FILTER: progid:DXImageTransform.Microsoft.Alpha(Opacity='.($params->get('categorymenu_out', 0.8)*100).');
}
');
echo '';
drawVerticalMenu($rows, $params->get('show_subcats', 1));
echo 'get('categorymenu_out', 0.8).',over : '.$params->get('categorymenu_over', 1).',duration : '.$params->get('categorymenu_fade', 300).',id : '.$GLOBALS['vertical_menu'].',width : '.$params->get('categorymenu_width', 150).'});" alt=""/>';
echo '';
?>
The problem is at the end:
echo '';
drawVerticalMenu($rows, $params->get('show_subcats', 1));
echo 'get('categorymenu_out', 0.8).',over : '.$params->get('categorymenu_over', 1).',duration : '.$params->get('categorymenu_fade', 300).',id : '.$GLOBALS['vertical_menu'].',width : '.$params->get('categorymenu_width', 150).'});" alt=""/>';
echo '';
?>
I think the extension uses the onload event to display the sub-menus when the user moves the mouse pointer over it.
What I want is to replace the onload with other event handler or some other label that is supported by XHTML and that is as similar to onload.
I would appreciate your help ...
You can remove the onload attributes and then add some simple javaScript to your page to run some functions when the page finishes loading.
It would look something like this:
<script>
window.onload=function(){
new WW.VerticalMenu({out : 1,over : 1,duration : 300,id : 0,width : 180}); // example of one of the functions being run
};
</script>
Good luck with your studies
It appears the source comes out like this right
<img src="/modules/mod_vertical_menu/images/center.gif" style="display:none" onload="new WW.VerticalMenu({out : 1,over : 1,duration : 300,id : 0,width : 180});" alt=""/>
As that looks like imho an unsavoury method of instantiating the menu I would try and loose that attribute completely by removing the offending php echo section and instead add javascript to load
as this is using dojo maybe this will help -- sorry I do not use dojo
http://mail.dojotoolkit.org/pipermail/dojo-interest/2012-May/066106.html
so php wise this would be
echo '<img src="modules/mod_vertical_menu/images/center.gif" style="display:none" alt=""/>'; echo '</div>';
echo "<script>" ."javascript or dojo script here ".</script>"
I would even suggest removing the image and using the window onload advocated below if you can

Joomla if else for menu by ID or url

I have a situation whereby i want two of my pages to look different form the rest but they are not front pages. If they were it would be easy as the code below would do the trick.
$menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
echo 'This is the front page';
esle(do something else)
}
?>
In short i want a similar approach but this time, to get the menu by ID/URL. Any ideas?
I got the answer to this...all you need to check is the menu ID then put the code below.
<?php
//This is the code for the page with menu ID 6
$menuID = JSite::getMenu()->getActive()->id ;
if ($menuID == '6')
{
echo '';
}
elseif ($menuID == '2') //This is the HTML for page with menu ID 2
{
echo '';
}
elseif ($menuID == '4') //THis is the html for page with menu id 4
{
echo '';
}
else //This is the HTML code for the rest of the pages
{
echo '';
}
?>
Page ItemId can be obtained using $itemid = JRequest::getInt( 'Itemid' );

Categories