PHP - Easier Way To Write This Simple Code?
<?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. Similar TutorialsHello, 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--> Something I have never done, but want to learn the best practice for doing so, is a listener script. Example: one of our websites is in bespoke PHP and has two links that shows if there are any noticeboard 'notifications' or any new 'messages'. Ideally I'd like to show 1, 2, 3... etc as a count of how many are remaining for Messages. So if a new one comes in while they are on a page, that number literally changes while they are on the page. I assume this is with Ajax, and not straight forward. I don't know any DOM code or Javascript. But would appreciate some help please. Simon I have a web site where users can gain points, when they use my site. For example, if the user spends $10.00 then the user will get 10 points. In my web site there is a list of 10 retails (name). When the user clicks the link it will redirect to retailers website, where user can buy items. How can I find out how much the user has spent on the retailer? Thank you. Can you suggest a better way to write this code (I didn't create it):
<body onload="getParameterByName('url')"> <a href="" id="urllink" >Click Here</a> </body>so, I don't have to change the <body> tag? suggestions on how to write this code better I have a form that has 2 textboxes in it. The php listed below...confirms that the passwords match, confirms that there are at least 6 characters in in, confirms that there is at least 1 number. I am doing mostly if statements with it, but I was wondering if there was a better way to write. Code: [Select] <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form name="form1" method="post" action="<?PHP $_SERVER['PHP_SELF'] ?>"> <table width="100%" border="1"> <tr> <th scope="row"> <strong>New password: </strong> <input type="password" maxlength="15" name="password"></th> <th scope="row"> <strong>New password confirmed: </strong> <input type="password" maxlength="15" name="confirm_password"></th> <th scope="row"><input type="submit" name="update" value="Update Password" /></th> </tr> </table> </form> <?PHP //verify that the update button is pressed if (isset($_POST['update'])) { //verify that the passwords match if($_POST['password'] == $_POST['confirm_password']) { echo "passwords match"; echo "<br>"; } //verify that the password and password confirmation was entered elseif ((isset($_POST['password'])) && ($_POST['confirm_password'])) { echo "Password and confirm password set"; echo "<br>"; } //verify that the # of characters was entered elseif (strlen($_POST['password'])<6) { $num_char=strlen($_POST['password']); echo "Please enter more characters"; echo "<br>"; } //find out if number exsist if (strlen($_POST['password'])>5) { $subject = $_POST['password']; preg_match_all('/[0-9]/', $subject, $matches); $count = count($matches[0]); //echo $count; if ($count <=1) echo "Please enter in at least 1 number you currently have: " .$count; }//close of verification of both boxes entered }//close of $_POST ?> </body> </html> This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=327190.0 Hi there, I've looked into the PHP superglobal array $_SERVER, but could not find a variable that stores the plugins a client has on his/her browser such as firefox and google chrome. The point here is to detect if a user has hacking addons such as firebug and inspect element installed, and displays an error message telling the user to disable such plugins in order to access site content. Is it possible to accomplish such tasks? Please help. this is an array returned after parsing YAMl file using spyc api. i want to write a code to to extract value related to key topic name and put in a separate 1d array say $topicnames Array ( [1] => Array ( [topicname] => singapore travels [rev] => 250 => Array ( [25] => Array ( ) [topicname] => airlines [rev] => 24 ) [1] => Array ( [35] => Array ( ) [topicname] => shubham [rev] => 65 ) ) [2] => Array ( [topicname] => singapore entertainment [rev] => 210 => Array ( [45] => Array ( ) [topicname] => night safari[rev] => 54 ) i tried using simple for loop but not working.. pl help me out. I have a small project. I have a control board that is connected to the parallel port. It controls 8 strips of LED on the LPT1 port. I can turn these on and off via the command prompt, using LPTOUT.exe ie lptout 1 turns on the 1st LED lptout 2 will turn off 1st LED and turn on 2nd, lptout 4 turns on 3rd and 2nd off, etc 1, 2, 4, 8, 16, 32, 64, 128. and combinations ie lptout 3 turns on LEDS 1 and 2, and so on. However lptout seems very unresponsive sometimes I have to do lptout 1 twice before LED 1 turns on. Is there another command I can use to write to LPT1 port. I need some PHP code to control the outputs. I want to sequence the outputs ie 1on (1second) 2on (1 second) 3 on (1 second) 4 on etc. to 8, then all off then repeat. Can anyone help me. THANK YOU. I am a complete newbie. I have installed xampp. I created a database and the tables but that is all. I need to create all my relationships and test my database. to do this, do I now need to write the php code to view the results in a browser or... do I do it in MySQL. If so, Can someone just point me in the direction I need to go for my next step.
Thanks
Here 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>";} ?> $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? 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 { } 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 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. Hi, this is my first time posting here. I am just delving into PHP and I am learning about foreach loops. I have written code in Notepad++ EXACTLY the way I saw it in a tutorial video I watched (I wish I could show the tutorial video to you, but it is on Lynda.com and you have to pay to watch) I attached the file with my code. The example 1 code works just fine. The example 2 code is the one that is not working for some reason. However, it worked for the guy that wrote it in the video, so I am not sure where I am going wrong? *The comments in green are mainly for myself, I explain things to myself so that I don't forget what the code does forloops.php 1.74KB 2 downloads I would appreciate some help. Thank you!!! I have a form that users input user name, password, and email... all writes to csv fine. My problem is I need to concatenate a a string with a variable and some html code will preserving the html when written to the csv.. I need the csv to be this username,password,email,user,category,text with http://sub.domain..com/splash/,something,something here's what i have Code: [Select] <?php if($_POST['formSubmit'] == "Submit") $varUserName = $_POST['username']; $varPW = $_POST['PW']; $varEmail = $_POST['email']; { $fs = fopen("testcsv.csv","a"); fputcsv($fs, array($varUserName,$varPW,$varEmail,"user","title",",category","some text '<a href="http://$varUserName.url.com/splash/>site.com</a>',)); fclose($fs); exit; } ?> and of course I'm getting Parse error: syntax error, unexpected T_STRING, expecting ')' on line 9 |