PHP - Have I Correctly Understood/translated Code Into Ternary Operators Shorthand?
Please bear with me in the following exercise. I'm fairly new to php (but not coding) and am trying to push my skills for the future, so I'm going beyond a problem for which I already have a solution to see if I understand the coding principles correctly.
I am trying to understand just how far I can push the ternary operator shorthand. I know how to do simple tasks, but I'm wondering if I can perform more than one command in the same statement? I'm a little confused because I'd like to both set a session variable and do a page redirect and I have a feeling that the order of these operations matters, so all I've done here may be incorrect. I have a form on the front page of my site that includes a checkbox that is required in order to enter the site. The form action goes to a page where this is checked, a $_SESSION variable is set, and the page is redirected. I am also not sure if I need the session_write_close(); command, or if it is in the right place. In the full length version, it could be moved into the set of commands following the IF. Here is the full length code that I would like to translate into shorthand: Code: [Select] <?php session_start(); if (isset($_POST['ConsentCheckbox']) { $_SESSION['Consented'] = 'true'; header('Location: http://www.mySite/welcome'); } else { header('Location: http://www.mySite/errors/NotConsented'); } session_write_close(); ?> The if/else conditions translate easily into these two lines of code, which could be one line if I didn't also need to set the session variable: Code: [Select] <?php session_start(); $_SESSION['Consented'] = isset($_POST['ConsentCheckbox']) ? 'true' : 'false'; header('Location: http://mySite.com/'.($_SESSION['Consented'] ? 'welcome' : 'error/NotConsented')); session_write_close(); ?> But, I really only need to set the $_SESSION variable if the $_POST['ConsentCheckbox'] is set. This makes the test on the following pages of my site much simpler (where I test to see if the $_SESSION variable is set and then either redirect to the consent form or load the desired page). SO, I'm wondering if it can be further condensed. I've come up with these possibilities, but am not totally sure. This has now become a theoretical exercise in coding for me. Here is what I think is possible, but I have a couple of outstanding questions, so I'd like some input, please. I first translated it into the following. I think this is correct, although I'm not sure if I need brackets around the multiple commands in the TRUE section. Code: [Select] <?php session_start(); isset($_POST['ConsentCheckbox']) ? $_SESSION['Consented']='true'; header('Location: http://www.mySite.com/welcome') : header('Location: http://www.mySite/errors/NotConsented'); session_write_close(); ?> Then, I wondered if I can even further condense it in the following way? Here is where the order of the commands is likely to break the code - and I'm not entirely sure that my use of double quotes will suffice instead of escaping out the end bracket of the header() command: Code: [Select] <?php session_start(); header('Location: http://www.mySite.com/'.(isset($_Post['ConsentCheckbox']) ? "welcome')"; $_SESSION['Consented'] = 'true' : "errors/NotConsented')"); session_write_close(); ?> Have I gone too far? Thanks Similar TutorialsHello, helpful readers: You all have been great for me in the past.... How do I handled index.php file unable to print the header.php or footer.php, for instance because of the version conflict. Have transferred all files from Yahoo, to Apache 2.2.17 web server. Everything else is taking shape nicely, thanks largely to the advice received here. Thanks for any info, Yshua if($AClass->select_error || $BClass->ErrorFile){ include("/path/to/files/".($AClass->select_error ? 'no_selector' : 'error_file_missing').".php"); exit; } if($CClass->WriteError){ include("/path/to/files/FileWriteError.php"); exit; } Probably a simple one for you folks, but I can't for the life of me work out how to make the above into 1 line of ternarys. Cheers, Rw In this statement... $firstName = (isset($_SESSION['memberFirstName']) ? $_SESSION['memberFirstName'] : ''); Can isset() be replaced with most other functions, e.g. empty(), is_numeric(), is_null(), etc?? Thanks, Debbie I have just discovered ternary conditionals and really like the time and space saving, they seem pretty simple and look neater than a bunch of if else statements crammed together. The thing I would like to know is what is the best or correct method of usng the following; $page = isset($_GET['page']) ? $_GET['page'] : '1'; or do I need the extra parentheses like below; $page = (isset($_GET['page'])) ? $_GET['page'] : '1'; I understand that if I used a second conditon I would need the extra parentheses e.g $page = (isset($_GET['page']) && is_numeric($_GET[page'])) ? $_GET['page'] : '1'; Thanks! I'm getting the following error with the code below: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in C:\wamp\www\flow\fpdf\rev.php on line 47 Can I get another set of eyes to look at it & see what I'm missing? Code: [Select] <html <head> <?php require('connection.php'); if (isset($_GET['op']) && $_GET['op'] == "d") if($_GET['op'] == "d" && !empty($_GET['id']) ) { { $result = mysql_query($query) or die(mysql_error()); } $query="SELECT fid, pacts, fname, lname, employee, dock, due_owner, due_attny, due_suspo, due_clerk, interv_date, due_rev, assgn_date, sent_date FROM psrinfo"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array( $result )) { ?> <?php } require('connection.php'); if (isset($_GET['op']) && $_GET['op'] == "d") if($_GET['op'] == "d" && !empty($_GET['id']) ) { $result = mysql_query($query) or die(mysql_error()); } $query="SELECT fid, pacts, fname, lname, employee, dock, due_owner, due_attny, due_suspo, due_clerk, interv_date, due_rev, assgn_date, sent_date FROM psrinfo WHERE employee = 'employee1' "; $result = mysql_query($query) or die(mysql_error()); echo '<form action="111.php" method="post"> <h2> <center> Paper Review </center></h2> <table width="75%" border="1">'; while($row = mysql_fetch_array( $result )) { echo '<tr> <td><b>PACTS:</b> <br><input type="text" name="pacts" size="25" maxlength="30" value="'. $row['pacts'] .'" /><br> </td> <td><b>Reviewer:</b> <br><input type="text" name="dock" size="25" maxlength="30" value="''" /><br> </td> </tr> <tr> <td><b>First Name:</b> <br><input type="text" name="fname" size="25" maxlength="30" value="'. $row['fname'] .'" /><br> </td> <td><b>Last Name:</b> <br><input type="text" name="lname" size="25" maxlength="30" value="'. $row['lname'] .'" /><br> </td> <td><b>Dock:</b> <br><input type="text" name="dock" size="25" maxlength="30" value="'. $row['dock'] .'" /><br> </td> </tr> <tr> <td><b>Sent Date:</b> <br><input type="text" name="sent_date" size="25" maxlength="30" value="'. $row['sent_date'] .'" /><br> </td> <td><b>Assign Date:</b> <br><input type="text" name="assgn_date" size="25" maxlength="30" value="'. $row['assgn_date'] .'" /><br> </td> <td><b>Interview Date:</b> <br><input type="text" name="interv_date" size="25" maxlength="30" value="'. $row['interv_date'] .'" /><br> </td> </tr> <tr> <td></b><br><input type="text" name="assgn_date" size="30" maxlength="30" value="Given to Reviewer" /> <br><input type="text" name="assgn_date" size="30" maxlength="30" value="Given to SUSPO" /> <br> <input type="text" name="assgn_date" size="30" maxlength="30" value="Given to Clerk" /> <br> <input type="text" name="assgn_date" size="30" maxlength="30" value="PSR Disclosed to Attn" /> <br> <input type="text" name="assgn_date" size="30" maxlength="30" value="PSR Disclosed to owner" /> </td> <td><b>Initials</b> <br><input type="text" name="dock" size="30" maxlength="30" /> <br> <input type="text" name="dock" size="30" maxlength="30" /> <br><input type="text" name="dock" size="30" maxlength="30" /> <br> <input type="text" name="dock" size="30" maxlength="30" /> <br> <input type="text" name="dock" size="30" maxlength="30" /></td> <td><b>Due Date:</b> <br><input type="text" name="due_rev" size="25" maxlength="30" value="'. $row['due_rev'] .'" /> <br> <input type="text" name="due_suspo" size="25" maxlength="30" value="'. $row['due_suspo'] .'" /> <br> <input type="text" name="due_clerk" size="25" maxlength="30" value="'. $row['due_clerk'] .'" /> <br> <input type="text" name="due_attny" size="25" maxlength="30" value="'. $row['due_attny'] .'" /> <br> <input type="text" name="due_" size="25" maxlength="30" value="'. $row['due_owner'] .'" /> </td> <td><br><input type="text" name="assgn_date" size="30" maxlength="30" value="Given to Reviewer" /> <br><input type="text" name="assgn_date" size="30" maxlength="30" value="Given to SUSPO" /> <br> <input type="text" name="assgn_date" size="30" maxlength="30" value="Given to Clerk" /> <br> <input type="text" name="assgn_date" size="30" maxlength="30" value="PSR Disclosed to Attn" /> <br> <input type="text" name="assgn_date" size="30" maxlength="30" value="PSR Disclosed to Owner" /> </td> <td><b>Actual Date:</b><br><input type="text" name="assgn_date" size="30" maxlength="30" value="" /> <br> <input type="text" name="assgn_date" size="30" maxlength="30" value="" /> <br> <input type="text" name="assgn_date" size="30" maxlength="30" value="" /> <br> <input type="text" name="assgn_date" size="30" maxlength="30" value="" /> <br> <input type="text" name="assgn_date" size="30" maxlength="30" value="" /> </td> </tr>; <td> <b>Employee Notes:</b> <br> <textarea name = "notes" rows=9 cols=100></textarea> </td> </tr>'; } echo '</table> </form>'; mysql_close(); // Close the database connection. ?> </tbody> </table> hello i keep seeing Ternary Operators like this Code: [Select] $action = (empty($_POST['action'])) ? 'default' : $_POST['action']; but how would i turn this if statement in to a Ternary Operator ? Code: [Select] if($a == $b){ echo 'hello'; }else{ //do nothing } thanks Hey all, when job_title property is equal to null, I want this to happen: Welcome to the blog of John Merlino. If it is not null then: Welcome to the blog of John Merlino, a web designer. //where web designer refers to the value stored in job_title So I come up with this: Code: [Select] echo "Welcome to the blog of " . $blogger->first_name . ' ' . $blogger->last_name . (!is_null($blogger->job_title)) ? ', ' . $blogger->job_title . '.' : '.'; But when job_title is null, all the page renders is this: Code: [Select] , . That's right. Just a comma, then a space, and then a period. What am I missing here? Thanks for response. Hello! This is the code for the header of my blog. It isn't displaying the CSS correctly, and the links aren't working. I think there is something wrong with the PHP, but am totally blind. Any ideas? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>> <head profile="http://gmpg.org/xfn/11"> <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" /> <title><?php wp_title('//', true, 'right'); ?> <?php bloginfo('name'); ?></title> <meta name="description" content="<?php bloginfo('description'); ?>" /> <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" /> <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/colours.css" type="text/css"/> <link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" /> <link rel="alternate" type="application/atom+xml" title="<?php bloginfo('name'); ?> Atom Feed" href="<?php bloginfo('atom_url'); ?>" /> <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" /> <?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?> <?php wp_head(); ?> </head> <body id="top" class="<?php echo setColourScheme(); ?>"> <div id="header"> <div class="blog-name"><a href="<?php echo get_option('home'); ?>/"><?php bloginfo('name'); ?></a></div> <div class="description"><?php bloginfo('description'); ?> </div> <ul id="menu"> <li><a href="<?php echo get_option('home'); ?>/" <?php if(is_home()) {echo 'class="selected"';} ?>>HOME</a></li> <li><a href="http...com">MAG</a></li> <li><a href="http...com">ABOUT</a></li> <li class="last"><a href="http...com">SUBSCRIBE</a></li> </ul> <div class="clearboth"><!-- --></div> </div> </body> Thanks so much! Elvia <?php $filename = "text.txt"; $filename2 = "text2.txt"; $file = file($filename, FILE_SKIP_EMPTY_LINES); foreach($file as $cwb) { $ids[] = "\$id_to_save[] = ".$cwb.";"; } file_put_contents($filename2,implode($ids,"\n\r")); ?> it takes ids from a file and implodes to a new file its supposed to looked like this $id_to_save[] = id; $id_to_save[] = id; $id_to_save[] = id; $id_to_save[] = id; instead it turns out like this $id_to_save[] = 4234324 ; $id_to_save[] = 2342343 ; $id_to_save[] = 3423432 ; $id_to_save[] = 3243244; I've tried trim() but it hasnt seemed to work., any help is appreciated. Thanks <?php $fp = fopen ('data.txt', 'w'); fwrite($fp, $_POST['feedback']); fclose($fp); echo 'Done.'; ?> Why have I implemented $_POST['feedback'] incorrectly? I also tried: ".$_POST['feedback']." '".$_POST['feedback']."' '.$_POST['feedback']' .$_POST['feedback'] Problem - http://runehints.com/calculators/mining?rsname=gaogier&action=calculator So far all is correct, once you click calculate the problems start. If I do a little math, level 67 is the target, with 548028 xp, minus the current xp of 533469 that = 14559 14559 / 50 xp per coal = x amount of coal which is 291.18 coal. Now, my calculator is saying, 33173 is the answer. Which it clearly is not the case. The URL I get, http://runehints.com/calculators/mining?rsname=gaogier&action=calculate&cxp=533469&lvl=2192114 if you change the last part (2192114) to 548028 it nearly works out to be right, as it shows 291 coal. http://runehints.com/calculators/mining?rsname=gaogier&action=calculate&cxp=533469&lvl=548028 I plan to get this script rewritten over the next couple of months, is it worth fixing it? I mean can you spot the error? <?php preg_match('~calculators\/([a-zA-Z]+)~', $_SERVER['REQUEST_URI'], $which); $which = $which[1]; ?> <div id="contenttitle"><?php echo ucfirst($which); ?> Calculator</div> <div class="news"> <?php preg_match('~(agility|attack|combat|cooking|crafting|defence|farming|firemaking|fishing|fletching|herblore|hitpoints|magic|mining|prayer|ranged|runecrafting|slayer|strength|summoning|thieving|woodcutting)~', $_SERVER['REQUEST_URI'], $url); $uri = $url[1]; if (empty($uri)) { echo '<table class="guide"> <tr class="trtitle"> <th class="main">Skill Name</th> <tr> <tr> <td><a href="/calculators/agility">Agility</a></td> <tr> <tr> <td><a href="/calculators/attack">Attack</a></td> <tr> <tr> <td><a href="/calculators/cooking">Cooking</a></td> <tr> <tr> <td><a href="/calculators/crafting">Crafting</a></td> <tr> <tr> <td><a href="/calculators/defence">Defence</a></td> <tr> <tr> <td><a href="/calculators/farming">Farming</a></td> <tr> <tr> <td><a href="/calculators/fishing">Fishing</a></td> <tr> <tr> <td><a href="/calculators/fletching">Fletching</a></td> <tr> <tr> <td><a href="/calculators/herblore">Herblore</a></td> <tr> <tr> <td><a href="/calculators/hitpoints">Hitpoints</a></td> <tr> <tr> <td><a href="/calculators/magic">Magic</a></td> <tr> <tr> <td><a href="/calculators/mining">Mining</a></td> <tr> <tr> <td><a href="/calculators/prayer">Prayer</a></td> <tr> <tr> <td><a href="/calculators/ranged">Ranged</a></td> <tr> <tr> <td><a href="/calculators/runecrafting">Runecrafting</a></td> <tr> <tr> <td><a href="/calculators/slayer">Slayer</a></td> <tr> <tr> <td><a href="/calculators/strength">Strength</a></td> <tr> <tr> <td><a href="/calculators/summoning">Summoning</a></td> <tr> <tr> <td><a href="/calculators/thieving">Thieving</a></td> <tr> <tr> <td><a href="/calculators/woodcutting">Woodcutting</a></td> <tr> </table>'; } else { # All Calculator - calculate.php # Orignally created on 28 December 2004 remade using RS Hiscores on 9 June 2005 # Created by Joseph Jeffery of DSIBaN Designs //This page functions as every calculator //set page headers $lvl = 0; preg_match('~rsname=(.*)&action~', $_SERVER['REQUEST_URI'], $rsnam); $rsn = $rsnam[1]; /** * Return current xp for specific skill */ function return_exp($name, $skill) { $url = "http://hiscore.runescape.com/index_lite.ws?player=" . strtolower(str_replace(' ', '_', $name)); if (($page = @file_get_contents($url)) === false) { echo "User $name was not found in the highscores for $skill"; return false; } if(strpos($page, "<html>")) { trigger_error('Unable to parse RuneScape highscore page', E_USER_WARNING); echo "<h1>DUMP</h1>"; } $catagories = split(' ', $page); //Find proper index for skill switch($skill) { case "attack": $ind = 1; break; case "defence": $ind = 2; break; case "strength": $ind = 3; break; case "constitution": $ind = 4; break; case "ranged": $ind = 5; break; case "prayer": $ind = 6; break; case "magic": $ind = 7; break; case "cooking": $ind = 8; break; case "woodcutting": $ind = 9; break; case "fletching": $ind = 10; break; case "fishing": $ind = 11; break; case "firemaking": $ind = 12; break; case "crafting": $ind = 13; break; case "smithing": $ind = 14; break; case "mining": $ind = 15; break; case "herblore": $ind = 16; break; case "agility": $ind = 16; break; case "thieving": $ind = 17; break; case "slayer": $ind = 18; break; case "farming": $ind = 20; break; case "runecrafting": $ind = 21; break; case "hunter": $ind = 22; break; case "construction": $ind = 23; break; case "summoning": $ind = 24; break; case "dungeoneering": $ind = 25; break; } $skill_info = split(",", $catagories[$ind]); $skill_exp = $skill_info[2]; return $skill_exp; } function enter_name(){ $skill = $_GET['skill']; $mem = $_GET['members']; $_GET['action'] = 'calculator'; preg_match('~rsname=(.*)&action~', $_SERVER['REQUEST_URI'], $rsnam); $rsn = $rsnam[1]; preg_match('~&action=(.*)~', $_SERVER['REQUEST_URI'], $action); $_GET['action'] = $action[1]; echo '<form action="" method="get"> <p>Runescape name: <input type="text" name="rsname" class="text" value="' . $rsn . '" /> <input type="hidden" name="action" value="calculator" /></p> <input type="submit" class="text" value="Grab XP" onMouseOver="this.style.cursor=\'hand\';"/> </form><br /> '; } function calculator(){ preg_match('~rsname=(.*)&action~', $_SERVER['REQUEST_URI'], $rsnam); $rsn = $rsnam[1]; preg_match('~calculators\/(.*)\?rsname~', $_SERVER['REQUEST_URI'], $skill); $skill = $skill[1]; preg_match('~&action=(.*)~', $_SERVER['REQUEST_URI'], $action); $_GET['action'] = $action[1]; if($rsn != "") { $xp = return_exp($rsn, $skill); } $mem = $_GET['members']; if ($xp > 13034430){ echo 'You\'re already level 99!'; } ?> <form action="" method="get"> <table border="0" class="calcinput"> <input type="hidden" name="rsname" value="<?php echo $rsn; ?>"> <input type="hidden" name="action" value="calculate" /> <tr><td>Current Xp:</td><td> <input type="text" name="cxp" class="text" value="<?php if ($xp != "0") { echo $xp; } ?>" /></td></tr> <tr><td>Current Level:</td><td><input type="text" class="text" value="<?php echo getLevel($xp); ?>" /></td></tr> <tr><td>Desired Level:</td><td> <SELECT name="lvl" class="text"><?php genDropDown($xp); ?> </select> <?php if ($mem == "yes"){ echo '<tr><td>Show members items?:</td><td> <input name="showmembers" type="checkbox" disabled="true" value="yes" checked></td></tr> '; }else{ echo '<tr><td>Hide members items?:</td><td> <input name="showmembers" type="checkbox" value="no"></td></tr> '; } ?> <tr><td><input type="submit" class="text" value="Calculate" onMouseOver="this.style.cursor='hand';"/></td><td> <input type="reset" class="text" value="Reset" onMouseOver="this.style.cursor='hand';"/></td></tr> </table> <?php } /*Calculate level based on experience function getLevel($xp) { $a = 0; for($i = 0; $i < 100; $i++) { if($a / 4 > $xp) { return $i; } $a += floor($i + 300 * pow(2, ($i / 7))); } } */ function getLevel($exp){ $levels = array(1 => 0, 83, 174, 276, 388, 512, 650, 801, 969, 1154, 1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523, 3973, 4470, 5018, 5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363, 14833, 16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224, 41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721, 101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254, 224466, 247866, 273742, 302288, 333804, 368599, 407015, 449428, 496254, 547953, 605032, 668051, 737627, 814445, 899256, 992895, 1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1968068, 2192818, 2421087, 2673114, 2951373, 3258594, 3597729, 3972294, 4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7994614, 8771558, 9684577, 10692629, 11805606, 13034431); $l = count($levels); while ($levels[$l] > $exp) $l--; return $l; } //Generate dropdowns based on xp function genDropDown($xp) { //Gather experience $a = 0; for($i = 0; $i < 100; $i++) { if($a / 4 > $xp) { if($lvl == 0) { $lvl = $i; } echo "<option value=\"$a\">Level $i - " . floor($a / 4) . " xp</option>"; } $a += floor($i + 300 * pow(2, ($i / 7))); } } function calculate(){ preg_match('~&lvl=(.*)~', $_SERVER['REQUEST_URI'], $lvl); $lvl = $lvl[1]; preg_match('~&cxp=(.*)&lvl~', $_SERVER['REQUEST_URI'], $cxp); $cxp = $cxp[1]; preg_match('~rsname=(.*)&action~', $_SERVER['REQUEST_URI'], $rsnam); $rsn = $rsnam[1]; preg_match('~calculators\/(.*)\?rsname~', $_SERVER['REQUEST_URI'], $skills); $skills = $skills[1]; preg_match('~&action=(.*)~', $_SERVER['REQUEST_URI'], $action); $_GET['action'] = $action[1]; $lev = $_POST['lev']; $xpn = $lvl - $cxp; if ($xpn == ""){ $xpn = $_GET['xpn']; } if ($lev == ""){ $lev = $_GET['lev']; } $show = $_GET['showmembers']; if ($show == "0"){ $show = $_POST['showmembers']; } switch($skills) { case 'firemaking': $skill = "fire"; $item = "Log"; break; case 'runecrafting': $skill = "rune"; $item = "Rune"; break; case 'ranging': $skill = "range"; $item = "Monster"; break; case 'herblore': $skill = "herb"; $item = "Herb/Potion"; break; case 'fishing': $skill = "fish"; $item = "Fish"; break; case 'cooking': $skill = "cook"; $item = "Food"; break; case 'woodcutting': $skill = "wood"; $item = "Log"; break; case 'agility': $skill = "agility"; $item = "Course"; break; case 'slayer': $skill = "slayer"; $item = "monster"; break; case 'farming': $skill = "farming"; $item = "Seed/Action"; break; case 'fletching': $skill = "fletching"; $item = "Item"; break; case 'magic': $skill = "magic"; $item = "Spell"; break; case 'mining': $skill = "mining"; $item = "Ore"; break; case 'smithing': $skill = "smithing"; $item = "Bar"; break; case 'thieving': $skill = "thieving"; $item = "Chest/Stall/Pickpocket"; break; case 'fighting': $skill = "fighting"; $item = "Monster"; break; case 'crafting': $skill = "crafting"; $item = "Item"; break; case 'prayer': $skill = "prayer"; $item = "Bones"; break; default: $skill = $skill[1]; $item = "Item"; } $mem = $_GET['members']; echo' <table border="0.5" cellspacing="1" cellpadding="2" align="center" bgcolor="#857553"> <tr><td class="tableheader"><a style="color: #EED8B4" href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=level&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers='.$show.'">Level needed</a></td><td class="tableheader"><a style="color: #EED8B4" href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=item&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers='.$show.'">'.$item.'</a></td><td class="tableheader"><a style="color: #EED8B4" href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=xp&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers='.$show.'">Experience given</a></td><td class="tableheader"><a style="color: #EED8B4" href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=xp&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers='.$show.'">Number of times</a></td><td class="tableheader"><a style="color: #EED8B4" href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=members&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers='.$show.'">Members?</a></td>'; require_once ('includes/connect.php');//connect to db $by = $_GET['orderby']; if ($mem == "no"){ if ($show == "no"){ $mess = '<a href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=level&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers=no">Click here to show members items</a>'; $n_query = 'SELECT * FROM `'.$skill.'calc` WHERE `members`="0" ORDER BY `'.$by.'` ASC'; }else{ $mess = '<a href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=level&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers=yes">Click here to hide members items</a>'; $n_query = 'SELECT * FROM `'.$skill.'calc` ORDER BY `'.$by.'` ASC'; } }else{ $n_query = 'SELECT * FROM `'.$skill.'calc` ORDER BY `'.$by.'` ASC'; } $n_result = mysql_query ($n_query); while ($n_row = mysql_fetch_assoc($n_result)) { $level = $n_row['level']; $course = $n_row['item']; $xpg = $n_row['xp']; $tot1 = $xpn / $xpg; $tot = sprintf('%.0f', $tot1); $members1 = $n_row['members']; if ($members1 == "1"){ $members = "Yes"; $bg2 = "#808080"; }else{ $members = "No"; $bg2 = "#FF8C00"; } if ($lev < $level){ $bg = "#049f2c"; }else{ $bg = "#006600"; } echo '<tr><td class="calc" bgcolor="'.$bg.'"onMouseOver="this.style.backgroundColor=\'#71828A\'" onMouseOut=this.style.backgroundColor="'.$bg.'">'.$level.'</td><td>'.$course.'</td><td>'.$xpg.'</td><td>'.$tot.'</td><td class="calc" bgcolor="'.$bg2.'" onMouseOver="this.style.backgroundColor=\'#71828A\'" onMouseOut=this.style.backgroundColor="'.$bg2.'">'.$members.'</td>'; } echo' </table><br /> '; echo $lev; } preg_match('~rsname=(.*)&action~', $_SERVER['REQUEST_URI'], $rsnam); $rsn = $rsnam[1]; preg_match('~calculators\/(.*)\?rsname~', $_SERVER['REQUEST_URI'], $skill); $skill = $skill[1]; preg_match('~&action=([a-zA-Z0-9]+)~', $_SERVER['REQUEST_URI'], $action); $_GET['action'] = $action[1]; switch($_GET['action']) { case 'calculate': enter_name(); calculate(); break; case 'calculator': enter_name(); calculator(); break; case 'enter_name': enter_name(); calculator(); break; default: enter_name(); } } ?> </div> Ok, as far as I know this code should update correctly, but for some reason it's not, and I can't seem to figure out why. Here is the page: http://kaboomlabs.com/testbed/edit.php?id=1 Mind you this is meant to be seen in IE only, so if it looks odd on your browser I haven't gotten to tweaking it yet.... thanks for the help. Code: [Select] <?php require_once('../connectvars.php'); echo '<div id="postwrap">' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PDI NCMR - Edit</title> <link rel="stylesheet" type="text/css" href="../CSS/postie.css" /> </head> <body> <div id="logo"> <img src="../images/PDI_Logo_2.1.gif" alt="PDI Logo" /> </div> <?php if (isset($_POST['submit'])) { // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); // Enter data into the database $ab = mysqli_real_escape_string($dbc, trim($_POST['ab'])); $date = mysqli_real_escape_string($dbc, trim(date('Y-m-d',strtotime ($_POST['date'])))); $part = mysqli_real_escape_string($dbc, trim($_POST['part'])); $rev = mysqli_real_escape_string($dbc, trim($_POST['rev'])); $partdesc = mysqli_real_escape_string($dbc, trim($_POST['partdesc'])); $ncmrqty = mysqli_real_escape_string($dbc, trim($_POST['ncmrqty'])); $comp = mysqli_real_escape_string($dbc, trim($_POST['comp'])); $ncmrid = mysqli_real_escape_string($dbc, trim($_POST['ncmrid'])); $rma = mysqli_real_escape_string($dbc, trim($_POST['rma'])); $jno = mysqli_real_escape_string($dbc, trim($_POST['jno'])); $fdt = mysqli_real_escape_string($dbc, trim($_POST['fdt'])); $cof = mysqli_real_escape_string($dbc, trim($_POST['cof'])); $fab1= mysqli_real_escape_string($dbc, trim($_POST['fab1'])); $fab2= mysqli_real_escape_string($dbc, trim($_POST['fab2'])); $fab3= mysqli_real_escape_string($dbc, trim($_POST['fab3'])); $non= mysqli_real_escape_string($dbc, trim($_POST['non'])); $dis= mysqli_real_escape_string($dbc, trim($_POST['dis'])); $comm= mysqli_real_escape_string($dbc, trim($_POST['comm'])); $caad= mysqli_real_escape_string($dbc, trim($_POST['caad'])); $po= mysqli_real_escape_string($dbc, trim($_POST['po'])); $pod = mysqli_real_escape_string($dbc, trim(date('Y-m-d',strtotime($_POST['pod'])))); $dri = mysqli_real_escape_string($dbc, trim(date('Y-m-d',strtotime($_POST['dri'])))); $query = "UPDATE ncmr SET ab = '$ab', date = '$date', part = '$part', rev = '$rev' , partdesc = '$partdesc' , ncmrqty = '$ncmrqty' , comp = '$comp' , ncmrid = '$ncmrid' , rma = '$rma' , jno = '$jno' , fdt = '$fdt' , cof = '$cof' , fab1 = '$fab1' , fab2 = '$fab2' , fab3 = fab3' , non = '$non' , dis = '$dis' , comm = '$comm' , caad = '$caad' , po = '$po' , pod = '$pod' , dri = '$dri' WHERE id = '" . $_GET['id'] . "'"; mysqli_query($dbc, $query); // Clear the form data $ab = ""; $date = ""; $part = ""; $rev = ""; $partdesc = ""; $ncmrqty = ""; $comp = ""; $ncmrid = ""; $rma = ""; $jno = ""; $fdt = ""; $cof = ""; $fab1= ""; $fab2= ""; $fab3= ""; $non= ""; $dis= ""; $comm= ""; $caad= ""; $po= ""; $pod = ""; $dri = ""; // Confirm success with the user echo '<p>If you wish to edit more NCMRs, please <a href="list.php">go to the admin page!</a></p>'; mysqli_close($dbc); } else { // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); // Grab the profile data from the database if (!isset($_GET['id'])) { $query = "SELECT * FROM ncmr WHERE id = '$id'"; } else { $query = "SELECT * FROM ncmr WHERE id = '" . $_GET['id'] . "'"; } $data = mysqli_query($dbc, $query); if (mysqli_num_rows($data) == 1) { // The user row was found so display the user data $row = mysqli_fetch_array($data); echo'<div id="title"><h3 id="NCMR2">Non-Conforming Materials Report (NCMR: ' . $row['rma'] . ')</h3></div>'; echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>"; echo '<fieldset>'; echo '<div id="box1">'; if (empty($row['ab'])) $row['ab'] = "Empty"; if (empty($row['date'])) $row['date'] = "Empty"; if (empty($row['part'])) $row['part'] = "Empty"; if (empty($row['rev'])) $row['rev'] = "Empty"; if (empty($row['partdesc'])) $row['partdesc'] = "Empty"; if (empty($row['ncmrqty'])) $row['ncmrqty'] = "Empty"; echo '<div id="ab"><span class="b">Added By: </span><input type="text" name="ab" value="' . $row['ab'] . '" /></div>'; echo '<div id="date"><span class="b">Date Filed: </span><input type="text" name="date" value="' . $row['date'] . '" /></div>'; echo '<div id="part"><span class="b">Part Number: </span><input type="text" name="part" value="' . $row['part'] . '" /></div>'; echo '<div id="rev"><span class="b">Part Revision: </span><input type="text" name="rev" value="' . $row['rev'] . '" /></div>'; echo '<div id="partdesc"><span class="b">Part Description: </span><textarea rows="4" cols="22">' . $row['partdesc'] . '</textarea></div>'; echo '<div id="ncmrqty"><span class="b">NCMR Qty: </span><input type="text" name="ncmrqty" value="' . $row['ncmrqty'] . '" /></div>'; echo '</div>'; //Company, Customer NCMR, Internal RMA, and Job Number echo '<div id="box2">'; if (empty($row['comp'])) $row['comp'] = "Empty"; if (empty($row['ncmrid'])) $row['ncmrid'] = "Empty"; if (empty($row['rma'])) $row['rma'] = "Empty"; if (empty($row['jno'])) $row['jno'] = "Empty"; echo '<div id="comp"><span class="b">Company: </span><input type="text" name="comp" value="' . $row['comp'] . '" /></div>'; echo '<div id="ncmrid"><span class="b">Customer NCMR ID: </span><input type="text" name="ncmrid" value="' . $row['ncmrid'] . '" /></div>'; echo '<div id="rma"><span class="b">Internal RMA #: </span><input type="text" name="rma" value="' . $row['rma'] . '" /></div>'; echo '<div id="jno"><span class="b">Job #: </span><input type="text" name="jno" value="' . $row['jno'] . '" /></div>'; echo '</div>'; //Type of Failure and Class of Failure echo '<div id="box3">'; echo '<h2>Failure</h2>'; echo '<div id="cof"><span class="b">Class of Failu </span><input type="text" name="cof" size="15" value="' . $row['cof'] . '" /></div>'; echo '<div id="fdt"><span class="b">Failure Due To: </span><input type="text" name="fdt" size="15" value="' . $row['fdt'] . '" /></div>'; echo '</div>'; //Fabricators echo '<div id="box4">'; echo '<h2>Fabricators</h2>'; if ($row['fab1']="--None--") { echo'<div id="fab1">'; $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $mysqli->select_db('user'); $result = $mysqli->query("SELECT * FROM user"); echo "<SELECT name='fab1'>\n"; while($row = $result->fetch_assoc()) { echo "<option value='{$row['user']}'>{$row['user']}</option>\n"; } echo "</select>\n"; echo '</div>'; } else { echo'<div id="fab1">'; $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $mysqli->select_db('user'); $result = $mysqli->query("SELECT * FROM user"); echo "<SELECT name='fab1'>\n"; while($row = $result->fetch_assoc()) { echo "<option value='{$row['user']}'>{$row['user']}</option>\n"; } echo "</select>\n"; echo '</div>'; } if ($row['fab2']="--None--") { echo'<div id="fab2">'; $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $mysqli->select_db('user'); $result = $mysqli->query("SELECT * FROM user"); echo "<SELECT name='fab2'>\n"; while($row = $result->fetch_assoc()) { echo "<option value='{$row['user']}'>{$row['user']}</option>\n"; } echo "</select>\n"; echo '</div>'; } else { echo '<div id="fab2"><span class="b"></span><input type="text" name="fab1" size="20" value="' . $row['fab1'] . '" /></div>'; echo '</div>'; } if ($row['fab3']="--None--") { echo'<div id="fab3">'; $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $mysqli->select_db('user'); $result = $mysqli->query("SELECT * FROM user"); echo "<SELECT name='fab3'>\n"; while($row = $result->fetch_assoc()) { echo "<option value='{$row['user']}'>{$row['user']}</option>\n"; } echo "</select>\n"; echo '</div>'; } else { echo '<div id="fab3"><span class="b"></span><input type="text" name="fab1" size="20" value="' . $row['fab1'] . '" /></div>'; echo '</div>'; } echo '</div>'; //Nonconformity, Disposition, Comments and Comments & Additional Details echo '<div id="box5">'; if (empty($row['non'])) $row['non'] = "Empty"; if (empty($row['dis'])) $row['dis'] = "Empty"; if (empty($row['comm'])) $row['comm'] = "Empty"; if (empty($row['caad'])) $row['caad'] = "Empty"; echo '<div id="non"><span class="b">Nonconformity: </span><textarea rows="4" cols="105">' . $row['non'] . '</textarea></div>'; echo '<div id="dis"><span class="b">Disposition: </span><textarea rows="4" cols="105">' . $row['dis'] . '</textarea></div>'; echo '<div id="comm"><span class="b">Comments: </span><textarea rows="4" cols="105">' . $row['comm'] . '</textarea></div>'; echo '<div id="caad"><span class="b">Comments and/or Additional Details: </span><textarea rows="4" cols="105">' . $row['caad'] . '</textarea></div>'; echo '<div id="podr">'; if (empty($row['po'])) $row['po'] ="Empty"; if (empty($row['pod'])) $row['pod'] ="Empty"; if (empty($row['dir'])) $row['dri'] ="Empty"; echo '<div id="po"><span class="b">PO: </span><input type="text" name="po" size="7" value="' . $row['po'] . '" /></div>'; echo '<div id="pod"><span class="b">PO Date: </span><input type="text" name="pod" size="7" value="' . $row['pod'] . '" /></div>'; echo '<div id="dri"><span class="b">Date Received: </span><input type="text" name="dri" size="7" value="' . $row['dri'] . '" /></div>'; echo '</div>'; echo '<div id="button2"><input type="submit" value="Submit Edits" name="submit" /></div>'; echo '</div>'; echo '</fieldset>'; echo '</form>'; } } echo '</div>'; ?> </body> </html> I always have trouble reading shorthand statements, as i am used to always using brackets. So when i view someones code and they use shorthand, i get confused. Can i get a clear explanation on the example below:
if ( have_posts() ) : while ( have_posts() ) : the_post(); the_content(); endwhile; else: echo "<p>Sorry, no posts matched your criteria.</p>"; endif;I've always been accustomed to using brackets, so i have trouble understanding the above quickly. *Mods* PLEASE don't move this to the OOP section again. I've had no luck there, and it's not specifically an OOP question. Hi everyone, I just want to know if there's a shorthand way to create objects, as there is for arrays: Code: [Select] // shorthand array creation $foo = array( 'bar' => 0, 'baz' => array ( 'name' => 'Dave', 'gender' => 'male' ) ) Code: [Select] // is there a way to do this with objects? // not that I know so far... $foo = object( bar->0, 'baz' -> object ( name -> 'Dave', gender -> 'male' ) ) I find object-access syntax is often quicker and easier to write than array-access syntax. Have I missed something obvious that I can't just write it out this way? Cheers, Dave Hi guys! I am trying to understand why the final value of $m equals 40 in the following code Code: [Select] <?php $i = 29; $j = 11; $m = 10; $j = ($j - 4) / 2; $m += $j * 10; echo "m = ".$m."<br>"; //Equals 40 ?> Can anyone explain me please? I feel very confused right now. I came across these pieces of code and have been trying to understand this operator -> Or if it is a operator. I have searched and couldn't find explanations. In trying to learn I need to know what is is used for. Is it used only with classes? $m->myMethod(); return $this->x; i'm trying to develop a deeper understanding of sessions, and I found this article on php.net regarding sessions with the following script;
<?php // Get the private context session_name('Private'); session_start(); $private_id = session_id(); $b = $_SESSION['pr_key']; session_write_close(); // Get the global context session_name('Global'); session_id('TEST'); session_start(); $a = $_SESSION['key']; session_write_close(); // Work & modify the global & private context (be ware of changing the global context!) ?> <html> <body> <h1>Test 2: Global Count is: <?=++$a?></h1> <h1>Test 2: Your Count is: <?=++$b?></h1> <h1>Private ID is <?=$private_id?></h1> <h1>Gloabl ID is <?=session_id()?></h1> <pre> <?php print_r($_SESSION); ?> </pre> </body> </html> <?php // Store it back session_name('Private'); session_id($private_id); session_start(); $_SESSION['pr_key'] = $b; session_write_close(); session_name('Global'); session_id('TEST'); session_start(); $_SESSION['key']=$a; session_write_close(); ?> [EDIT BY danbrown AT php DOT net: Contains a bugfix provided by (lveillette AT silexmultimedia DOT com) on 19-NOV-09.]I do not understand line 21 and 22. <h1>Test 2: Global Count is:<?=++$a?></h1>If I change this to; <h1>Test 2: Global Count is:<?php echo =++$a; ?></h1> or <h1>Test 2: Global Count is:<?php =++$a ?></h1>I would get a parse error, unexpected "=" in line 21. Can anyone explain why this is? * Why is it being echoed * Why can I not use echo in this case * Why does the code not start with <?php ? * What does line 21 even mean? =++$a? Thanks in advance guys! Edited by empec, 13 July 2014 - 11:30 AM. I'm trying to get an equation from a user that types it in the url, ex) 1+2+3. I can get the 1,2,3 fine, but the + operator is no longer in the string. Same with the * and / operators. Is there a way to keep them as a string using $_GET?
|