PHP - Finding An Easier Way...
Hi ,
I'm new on this forum so don't judge too hard.. But I have a few questions. I am recently new to PHP as this is my first computer language I've learnt besides HTML. My first question is, what's the difference between !== and !=, and is it more secure to use !== when comparing two fields such as passwords? I've different things and this has confused me.. And my second question is, I have this piece of code Code: [Select] /* option error checking */ $field = "option"; // Use field name for option /* Check if the option picked is from the list of options to choose from*/ if($suboption !== "option1" || $suboption !== "option2" || $suboption !== "option3" || $suboption !== "option4" || $suboption !== "option5" || $suboption !== "option6" || $suboption !== "option7"){ $form->setError($field, "* Unexpected error with option"); } I'm guessing in the long run, this will slow down my script.. is there an easier way of checking without using || $suboption !== and is it okay if I've used !==? Like I said I'm new to this so please don't flame.. I've done some research but I can't seem to find some accurate answer. Thank you, ZT Similar TutorialsHere is my long drawn out code. Is there a better way of doing this and how can I echo the JS without it messing up the php. I really need the JS link to work! <?php if(isset($_SESSION['rest'])){ echo'<table width="300" border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="middle" class="topnav"><div align="center" ><a href="index.php">Home</a></div></td> <td valign="middle" class="topnav"><div align="center"><a href="restpanel.php">User Panel</a></div></td> <td valign="middle" class="topnav"><div align="center"><a href="logout.php">Logout</a></div></td> </tr> </table>';} if(isset($_SESSION['chef'])){ echo' <table width="300" border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="middle" class="topnav"><div align="center" ><a href="index.php">Home</a></div></td> <td valign="middle" class="topnav"><div align="center"><a href="chefpanel.php">User Panel</a></div></td> <td valign="middle" class="topnav"><div align="center"><a href="logout.php">Logout</a></div></td> </tr> </table>';} if(!isset($_SESSION['chef']) || !isset($_SESSION['rest'])){ echo "<table width='350' border='0' cellpadding='0' cellspacing='0'> <tr> <td valign='middle' class='topnav'><div align='center'><a href='index.php'>Home</a></div></td> <td valign='middle' class='topnav'><div align='center' >"<a href="javascript:void(0)" onclick = "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'">Log In</a></div></td> <td valign="middle"class="topnav"><div align="center"><a href="register.php">Sign Up</a></div></td> <td valign="middle"class="topnav"><div align="center" ><a href="contact.php">Contact Us</a></div></td> </tr> </table>";} ?> Currently what I have is this:
$url="http://www.nfl.com/liveupdate/scorestrip/postseason/ss.xml"; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); // get the url contents $data = curl_exec($ch); // execute curl request curl_close($ch); $xml = simplexml_load_string($data); foreach($xml->gms->g as $games) { if(!empty($games['vtn'])) { if ((($games['gt']) =='WC') && (($games['q'])== 'F')) { echo "<li data-subcategory='Final Scores' data-category='Wild Card Games' data-color='1979ab'><a href='#'>",$games['htn']," ",$games['hs']," ",$games['vtn']," ",$games['vs']," Final</a></li>"; } if (($games['gt']) =='DIV') { echo "<li data-subcategory='Final Scores' data-category='Divisional Games' data-color='14ab05'><a href='#'>",$games['htn']," ",$games['hs']," ",$games['vtn']," ",$games['vs']," ",$games['d']," at ",$games['t'], "</a></li>"; } if (($games['gt']) == 'CON') { echo "<li data-subcategory='Conference Games' data-category='Sunday' data-color='000000'><a href='#'>",$games['htn']," ",$games['hs']," ",$games['vtn']," ",$games['vs']," ",$games['d']," at ",$games['t'], "</a></li>"; } } }It works great, Thanks to some help! The only thing I'd like to do now is have the output be a little more inline ...currently it reads the xml line by line and outputs the each item.. the order is the problem... It does this: Wild Card game Wild Card game Div game Div game Conference game Conference game Div game Div game Wild Card game Wild Card game I'd like it to output one type at a time....basically grouping all types together.. WC game Div game Conference game I continue to learn but this one is stumping me... I did get an idea from someone but not sure how to carry this one out... Instead of outputting it directly you could just add it to an array in the first loop So for each game you do $games[$type][] = $game; Then you can add another loop (for output) where you will have them sorted Just not sure how to handle that.. I'm feeling a bit lost at the moment... Is this in reference to putting $games[$type] = $game in place of foreach($xml->gms->g as $games)? I guess the second part has me lost as well adding another loop for output... needing to be sorted because each output is styled differently I guess I'm just not understanding.... can anyone clarify for me please? Thanks everyone! Whenever people are going to update something, they have a list of things to update. Whenever they choose the file they wish to edit/re-upload, they click the [EDIT] link beside it. This takes them to the update page, of course on the same file. They still have edit.php?id=3 in the URL, for example. But whenever they click Submit, how am I suppose to get the ID again? Is there a less junky way to do it then this: if(!$_GET['id'] && $_POST['id']) { $id = $_POST['id']; } elseif($_GET['id'] && !$_POST['id']) { $id = $_GET['id']; } else { } $text=$_POST['Txt']; $strrep1=str_replace('z', 'a', $text); $strrep2=str_replace('y', 'z', $strrep1); This code is a little bit time consuming to do I'm trying to make all the letters one higher, For example "Foo bar" becomes "Gpp Cbs" The reason I'm trying to do this is a long story, But anyway is there an easier way to do it reather than the way I was doing it? And would it work? I have about 350 rows that I have to update a single column in each row. I coded so I can do each row individually but I was wondering if there is a way to update all of them at once. Hence saving me time. This is my code: Code: [Select] <?PHP include "dbmembers.php"; ?> <?php if(isset($submit)){ $ID = mysql_real_escape_string(trim($_POST['ID'])); $result = mysql_query("SELECT response FROM myuser WHERE ID ='$ID'"); $row = mysql_fetch_array($result); $res = hash("sha256", $row['response'] .$salt ); { echo $row['response'] ."----".$res; echo "<br /><br />"; } } mysql_query("UPDATE myuser SET response='$res' WHERE ID = '$ID'"); printf ("Updated records: %d\n", mysql_affected_rows()); ?> Thanks in advance for any advise. Hello, I'm new to PHP and I have come up with the following code for my navigation but I am sure there has to be an easier and more efficient way to do this. Someone please start me on the right path. Thanks Code: [Select] <?php //Get selected page/sub page if (isset($_GET['page'])) { $sel_page = $_GET['page']; $sel_sub_page = ''; } elseif (isset($_GET['sub_page'])) { $sel_page = ''; $sel_sub_page = $_GET['sub_page']; } else { $sel_page = ''; $sel_sub_page = ''; } ?> Code: [Select] <!--Begin Navigation--> <ul> <?php //run the loop to get the page titles $query = "SELECT * FROM pages ORDER BY position ASC"; $page_set = mysql_query($query); confirm_query($page_set); while($page = mysql_fetch_array($page_set)){ echo '<li><a href="content.php?page=' . urlencode($page['id']) . '">' . $page['page_name'] . '</a></li>'; //run sub page query to see if the ul tag is needed $query = "SELECT * FROM sub_pages WHERE page_id = {$page['id']} ORDER BY position ASC"; $sub_page_set = mysql_query($query); confirm_query($sub_page_set); $sub_page = mysql_fetch_array($sub_page_set); if ($sub_page !=''){ echo '<ul>'; } //run the sub page loop again to display the sub page title $query = "SELECT * FROM sub_pages WHERE page_id = {$page['id']} ORDER BY position ASC"; $sub_page_set = mysql_query($query); confirm_query($sub_page_set); while($sub_page = mysql_fetch_array($sub_page_set)) { echo '<li><a href="content.php?sub_page=' . urlencode($sub_page['id']) . '">' . $sub_page['page_name'] . '</a></li>'; } //run the sub page loop for the last time to see if the end ul tag is needed $query = "SELECT * FROM sub_pages WHERE page_id = {$page['id']} ORDER BY position ASC"; $sub_page_set = mysql_query($query); confirm_query($sub_page_set); $sub_page = mysql_fetch_array($sub_page_set); if ($sub_page !='') { echo '</ul>'; } } ?> </ul> <!--End Navigation--> HI, My code works but I was wondering if there's a simpler/cleaner way to code it ... Code: [Select] <?php require_once('config.php'); // code to get data form database mysql_select_db($database, $makeconnection); $sql_get_categories = " SELECT * FROM tbl_categories ORDER BY category_id ASC"; $get_categories = mysql_query($sql_get_categories, $makeconnection) or die(mysql_error()); $row_get_categories = mysql_fetch_assoc($get_categories); $totalRows_get_categories = mysql_num_rows($get_categories); // i named the 3 fields form the form in the HTML so i can update them into the tadabase $category_1 = $_POST['category_1']; $category_2 = $_POST['category_2']; $category_3 = $_POST['category_3']; //updating the databse if (isset($_POST['submitted_categories'])&&($_POST['submitted_categories'] == "yes")) { $register_query = "SELECT category_id FROM tbl_categories WHERE category_id='$category_id'"; mysql_select_db($database, $makeconnection); $sql_modify1 = ("UPDATE `tbl_categories` SET `category_name` = '$category_1' WHERE `category_id` =1"); $sql_modify2 = ("UPDATE `tbl_categories` SET `category_name` = '$category_2' WHERE `category_id` =2"); $sql_modify3 = ("UPDATE `tbl_categories` SET `category_name` = '$category_3' WHERE `category_id` =3"); $Result1 = mysql_query($sql_modify1, $makeconnection) or die(mysql_error()); $Result2 = mysql_query($sql_modify2, $makeconnection) or die(mysql_error()); $Result3 = mysql_query($sql_modify3, $makeconnection) or die(mysql_error()); header ("Location: the-rest.php"); } ?> here's the form in the html part Code: [Select] <!--CATEGORIES --> <h2>Categories</h2> <form action="" method="post" enctype="multipart/form-data" name="category-form" id="category-form"> <?php do { ?> <h3><input class="user-form-input" id="category_<?php echo $row_get_categories['category_id'];?>" name="category_<?php echo $row_get_categories['category_id'];?>" type="text" value="<?php echo $row_get_categories['category_name'];?>" /></h3> <?php } while ($row_get_categories = mysql_fetch_assoc($get_categories)); ?> <input name="submitted_categories" type="hidden" id="submitted_categories" value="yes" /> <input name="submit" type="submit" class="button-save" id="submit" value="update categories"/> </form> <!--END OF CATEGORIES--> <?php $result = "SELECT * FROM portfolio"; $result = mysql_query ($result) or die (mysql_error()); $i=0; while($row = mysql_fetch_assoc($result)) { if($i==0) echo '<div class="portfolioPage">'; if($i==2) $divclass = 'portfolioProjectWrapper borderWhite'; else $divclass = 'portfolioProjectWrapper borderGray'; echo ' <div class="'.$divclass.'"> <a href="portfolioPage.html" class="image asyncImgLoad" title="img/'.$row['image290x290'].'"></a> <p class="imageDesc">'.$row['image290x290_phot'].'</p> <h3 class="title">'.$row['title'].'</h3> <p class="subtitle">'.$row['subtitle'].'</p> <p class="desc"> '.substr($row['description'], 0, 1200).' <a href="portfolioPage.html" class="commonLink">Read more</a> </p> </div> '; if($i==2) echo '</div>'; if(i==2) $i=0; else $i++; } ?> Please help me im backward at this been along time. Hi I have a file, that is copied from other files. All other files work perfectly. But, for some reason this one is throwing back an error. I've been over it so many time, but can't see whats wrong. Error: Fatal error: Call to undefined method stdClass::save() in /home/p/o/powtest/web/public_html/admin/lib/ajax_php/add_interests.php on line 28 add_interests.php Code: [Select] <?PHP require_once("../../../includes/initialize.php"); $flag = 0; $ID = $_POST['ID']; $type = $_POST['type']; $category = $_POST['category']; $interest = $_POST['interest']; $expInt = explode("\n", $interest); $DMOD = date('Y-m-d'); $TMOD = date('H:i:s'); $check_entry = Admin_interest::if_exists(clean_input_value($category)); if($check_entry == 0){ $new_category = Admin_interest::make($ID, clean_input_value($category), clean_input_value($type), $DMOD, $TMOD); if($new_category && $new_category->save()){ $CID = $new_category->id; $flag = 1; } } foreach($expInt as $expInts){ $check_entry = Admin_interests_sub::if_exists($CID, clean_input_value($expInts)); if($check_entry == 0){ $new_interest = Admin_interests_sub::make($ID, $CID, clean_input_value($expInts), $DMOD, $TMOD); if($new_interest && $new_interest->save()){ $message = 'Thank You: Your list Has Been Saved'; }else{ $message = "Sorry, There was an error"; } } } echo $message; ?> Class: Code: [Select] <?PHP require_once(LIB_PATH.DS.'database.php'); class Admin_interests_sub { protected static $table_name="admin_interests_sub"; protected static $db_fields = array('id', 'category_id', 'interest_sub', 'dateMod', 'timeMod'); public $id; public $category_id; public $interest_sub; public $dateMod; public $timeMod; public static function make($ID, $category_id, $interest_sub, $DMOD, $TMOD){ if(!empty($interest_sub)){ $interest = new Admin_interests_sub(); $interest->id = (int)$ID; $interest->category_id = (int)$category_id; $interest->interest_sub = $interest_sub; $kw->dateMod = $DMOD; $kw->timeMod = $TMOD; return $kw; }else{ return false; } } protected function attributes(){ $attributes = array(); foreach(self::$db_fields as $field){ if(property_exists($this, $field)){ $attributes[$field] = $this->$field; } } return $attributes; } protected function sanitized_attributes(){ global $database; $clean_attributes = array(); foreach($this->attributes() as $key => $value){ $clean_attributes[$key] = $database->escape_value($value); } return $clean_attributes; } public function save(){ return !empty($this->id) ? $this->update() : $this->create(); } public function create(){ global $database; $attributes = $this->sanitized_attributes(); $sql = "INSERT INTO ".self::$table_name." ("; $sql .= join(", ", array_keys($attributes)); $sql .= ") VALUES ('"; $sql .= join("', '", array_values($attributes)); $sql .= "')"; if($database->query($sql)){ $this->id = $database->insert_id(); return true; }else{ return false; } } public function update(){ global $database; $attributes = $this->sanitized_attributes(); $attribute_pairs = array(); foreach($attributes as $key => $value){ $attribute_pairs[] = "{$key}='{$value}'"; } $sql = "UPDATE ".self::$table_name." SET "; $sql .= join(", ", $attribute_pairs); $sql .= " WHERE id=".$database->escape_value($this->id); $database->query($sql); return ($database->affected_rows() == 1) ? true : false; } public function delete(){ global $database; $sql = "DELETE FROM ".self::$table_name." "; $sql .= "WHERE id=".$database->escape_value($this->id); $sql .= " LIMIT 1"; $database->query($sql); return ($database->affected_rows() == 1) ? true : false; } } ?> Any help finding this bug will be a big help. Thanks Okay I'm done with searching for answers, working on this preg_match for about 3 hours now.
I'm looking for "($0.01/$0.02 USD)" in a string. The needle might be slighty different. Possible strings that I might look for is: (€xx.xx/€xx.xx EUR) where the EUR can be changed into USD, including its signs. //calculate age $birthdate = "1978-04-26"; //birth date... actually being obtained from a database $today = date("Y-m-d H:i:s"); // The exact date $age = date_diff($str_birthday, $today); echo $age; I'd like a simple code to echo the age of someone with the mysql database information that's in their record. This doesn't work. I have no idea why. Nothing seems to work that I've found on the net. Please help. Thanks. Im trying to find the time between the last database entry and the current time and then echo it so it says something like '2 minutes ago'. I tried doing: $last_post_gap = strtotime('NOW') - strtotime($topic_info->topic_last_post_time); $last_post_gap = date('i', strtotime($last_post_gap)); $last_post = date('F j, Y', strtotime($topic_info->topic_last_post_time)); if ($last_post <= '- 1 DAY') { $last_post = 'Yesterday at '.date("g:i a", strtotime($topic_info->topic_last_post_time)); } if ($last_post <= '- 1 MINUTE') { $last_post = 'Less Than 1 Minute Ago'; } else { $last_post = $last_post_gap.' Minutes Ago'; } but that just displays 0 minutes ago regardless of the time. Is there anything im missing? What I want to be able to do is this: If I have a list of values in my database: 1 13 26 15 12 4 I do a query of this database and sort it asc so it looks like this: 1 4 12 13 15 26 I want to know how I can use php and/or sql query to find these values based off of "position" in the list. If I wanted the 3rd value in the list, that value would be 12. If i wanted the 2nd value in the list it would be 4. How would I go about doing this? Thank you in advance. My query is not finding the last recieptnum entry, it is finding the number 9 everytime for some odd reason. Im trying to incrementally increase this each time a reciept is created. $getreceiptnum = mysql_query("SELECT receiptnum FROM accounting WHERE agency = '$agency' ORDER BY receiptnum DESC LIMIT 1") or die(mysql_error()); $recieptarray = mysql_fetch_array($getreceiptnum); $recieptnum = $recieptarray['receiptnum']; echo $recieptnum; I am trying to find the URL to a directory TWO levels above the script level (if you know what I mean). For example.. the script sits at Code: [Select] http://www.domain.com/TestArea2/members/index.php BUT I need to find the path to Code: [Select] http://www.domain.com/TestArea2/ the nearest I get is... $domain = $_SERVER['HTTP_HOST']; $domain .= $_SERVER['REQUEST_URI']; echo $domain; // outputs www.domain.com/TestArea2/members/index.php Is there a simple method to do this or have I got to split the string and work backwards? Many thanks Hello. I think I have a little unique and difficult to solve problem that I really need to post here. I, as a counter-strike portal administrator, am running some CS servers. With automatical script, the server uploads demos (videos of gameplay) on FTP in this format: 101229154428.dem and so on. The number is a date in this format: year, month, day, hours, minutes, seconds => for the example it's 29th December 2010, 15h, 44m and 28s. Then I have a "report" page. The important part on it is that, there is a date in unix timestamp in each row like 1336837680 (29th december 2010, 15h, 47m, 00s). And now what I need: Find a demo that corresponds to the unix timestamp. If there is a timestamp 1336837680, it should find 101229154428.dem. You know what I mean, there is not one demofile, there are plenty. Just find the demo, that contains a record of the time used in timestamp. Pretty difficult, isn't it? I would love to see if anybody came up with anything. Best regards! Howdy Colleagues,
I was wondering whether you know ways/websites/channels/magic owls to how to help find a job in USA as a programmer as in for relocating there while the company pays for the Visa and the travel expenses?
My land-lord's son found a job as a programmer in Texas and the company was generous and kind enough to pay his travelling expenses, as well for his wife and kid.
I have an account in Monster & Indeed and I send CVs very often, but no luck so far.
And I want to go there to work and live, not like some people to rely on welfare, I have worked my whole life.
Thank you!
BR,
Stefany
User submits 1 or more refrerences via a form. On the action page I want to check if any id's on the page match any values submitted in $_POST. Code: [Select] $Ref = $_POST['Ref']; $source = file_get_contents( 'E:/wamp/www/Project/File.php' ); $document = new DOMDocument; $document->validateOnParse = true; $document->LoadHTML($source); for($i=0; $i<count($Ref); $i++) { $ID = $document->getElementById('$Ref[$i]'); } I have a table on the source page with the same id as a reference contained in $Ref, but getElementById is not seeing/recognsing it. No idea why not. hi there all i need the code that adds the url to a page like in the new forums cheers.
you get a image of that website and the url of the page
can anyone help please
thank you
like if i type www,google.com <<< you get a image and link automaticly..
Edited by php-real-degree, 15 September 2014 - 05:07 PM. |