PHP - Pair Array Results To Replace Images
hi, i am trying to visualize a table by replacing the fixed values with images.
For example, i have a visitor statistics table on mysql. Values on the config_os table are always in three letters. WI7 for Windows7, MAC for Macintosh. I hope I can tell this correctly. I have a display page which retrieves the values from database and displays them website statistics. But in order to make it more cool, i am telling to PHP that When you echo to divtable WI7, replace them with specified Windows7 icon image which has identical name as WI7.gif. Here is the code: Code: [Select] include("conn.php"); $sql="SELECT *, INET_NTOA(location_ip) AS location_ip FROM xx_log_visit AS location_ip ORDER BY idvisit DESC LIMIT 10"; // I am selecting all so you can't see config_os but it is there . I cropped unnecessary code for keeping it easy to read. $result=mysql_query($sql); $numofrows = @mysql_num_rows($result); while($row = mysql_fetch_array($result)) { $system = $row['config_os']; $SysName = array ("AND", "IPH", "LIN", "MAC", "UNK", "W2K", "W95", "W98", "WI7", "WME", "WNT", "WOS", "WS3", "WVI", "WXP"); $SysRename = array ("<img src='images/os/AND.gif'>","<img src='images/os/IPH.gif'>","<img src='images/os/LIN.gif'>","<img src='images/os/MAC.gif'>","<img src='images/os/UNK.gif'>","<img src='images/os/W2K.gif'>","<img src='images/os/W95.gif'>","<img src='images/os/W98.gif'>","<img src='images/os/wi7.gif'>","<img src='images/os/WME.gif'>","<img src='images/os/WNT.gif'>","<img src='images/os/WOS.gif'>","<img src='images/os/WS3.gif'>","<img src='images/os/WVI.gif'>","<img src='images/os/WXP.gif'>"); $showsystem= str_replace($SysName, $SysRename, $system); What I do here is simply use strreplace function. $showsystem = str_replace($SysName, $SysRename, $system); and in the display table, continue from code: Code: [Select] echo (" <div class='sub15'>$showsystem</div> " ); } So for example instead of writing WI7 as visitors os, it shows WI7.gif, cool hmm How can i automate this instead of writing each of them separately? Thanks for any help.. Similar TutorialsIf I have an array like this, what makes the most sense for inserting a key value pair after a given key? This is how I came up with to do it, after looking at the manual I'm not sure if there's a function I'm missing, because this seems way too complicated. Code: [Select] <?php function addKV($arr, $keyToFind, $addKey, $addValue){ $keys = array_keys($arr); $spot = array_search($keyToFind, $keys); $chunks = array_chunk($arr, ($spot+1)); array_unshift($chunks[1], array($addKey=>$addValue)); $arr = call_User_Func_Array('array_Merge', $chunks); return $arr; } $myArr = array('id'=>1, 'name'=>'Name', 'created'=>time(), 'modified'=>time()); $myArr = addKV($myArr, 'name', 'foo', 'bar'); ?>$myArr now looks like Code: [Select] array('id'=>1, name=>'Name', 'foo'=>'bar', 'created'=>time(), 'modified'=>time()); I have a form that will be used to edit various fields including images. Everything works on this form except the images. When I choose the image i want to replace and upload the new image the location of the others get erased on the MySQL database and I'm just left with the one I replaced. How can I just change the image I pick to replace without erasing the location of the others? Is there a way just to tell the script to only replace the image that corresponds with the image selected? Thank You The Form 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=utf-8" /> <title>FormListing </title> </head> <body>\<?php session_start(); if(empty($_SESSION['myusername'])){ // send them back to login header('location: index.php'); } $_SESSION['id']; $_SESSION['$myusername']; $id = (int)$_GET['id']; $id = substr($id, 0,5); if($id < 1 || $id > 99999) exit; $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name=""; // Database name $tbl_name=""; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar $id=$_GET['id']; // Retrieve data from database $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?> <form method="post" action="aptmodifyform.php" enctype="multipart/form-data"/> <input type="hidden" name="id" id="id" value="<?php echo $id; ?>" /> <table width="914" height="1234" border="0"> <tr> <td width="636" height="68"><div align="right"><span style="color: #F00">*</span>Title</div></td> <td width="11"><div align="center">:</div></td> <td colspan="3" style="text-align: left"><label for="title"></label> <div align="left"><span id="sprytextfield1"> <input name="title" type="text" value="<? echo $rows['title']; ?>" id="title" size="50" maxlength="50"/> </span></div></td> </tr> <tr> <td><div align="right">County</div></td> <td><div align="center">:</div></td> <td colspan="3" style="text-align: left"><label for="county"></label> <select name="county" id="county"> <option selected="selected" <?=($rows['county'] == 'Bronx') ? 'selected="selected"' : ''?>>Bronx</option> <option <?=($rows['county'] == 'Brooklyn') ? 'selected="selected"' : ''?>>Brooklyn</option> <option <?=($rows['county'] == 'Manhattan') ? 'selected="selected"' : ''?>>Manhattan</option> <option <?=($rows['county'] == 'Queens') ? 'selected="selected"' : ''?>>Queens</option> <option <?=($rows['county'] == 'Staten Island') ? 'selected="selected"' : ''?>>Staten Island</option> <option>-----------------</option> <option <?=($rows['county'] == 'Nassau') ? 'selected="selected"' : ''?>>Nassau</option> <option <?=($rows['county'] == 'Suffolk') ? 'selected="selected"' : ''?>>Suffolk</option> </select></td> </tr> <tr> <td><div align="right">Town</div></td> <td><div align="center">:</div></td> <td colspan="3" style="text-align: left"><label for="town"></label> <input name="town" type="text" value="<? echo $rows['town']; ?>" id="town" size="50" maxlength="30"/></td> </tr> <tr> <td><div align="right">Description<br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> </div></td> <td><div align="center"> <p>:<br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> </p> </div></td> <td colspan="3" style="text-align: left"><label for="description"></label> <textarea name="description" cols="70" rows="25" id="description"><?php echo $rows['description']; ?></textarea></td> </tr> <tr> <td style="text-align: right; color: #000;">Service</td> <td style="text-align: center">:</td> <td colspan="3" style="text-align: left"><label for="service"></label> <select name="service" size="1" id="service"> <option <?=($rows['service'] == 'Landlord') ? 'selected="selected"' : ''?>>Lanlord</option> <option <?=($rows['service'] == 'Property Mangement') ? 'selected="selected"' : ''?>>Property Mangement</option> <option <?=($rows['service'] == 'Realtor') ? 'selected="selected"' : ''?>>Realtor</option> </select></td> </tr> <tr> <td><div align="right">Service Fees</div></td> <td>:</td> <td colspan="3" style="text-align: left"><select name="feeornofee" id="feeornofee"> <option <?=($rows['feeornofee'] == 'Broker Fees + Other Fees') ? 'selected="selected"' : ''?>>Broker Fees + Other Fees</option> <option <?=($rows['feeornofee'] == 'Broker Fees') ? 'selected="selected"' : ''?>>Broker Fees</option> <option <?=($rows['feeornofee'] == 'Other Fees - No Broker Fee') ? 'selected="selected"' : ''?>>Other Fees - No Broker Fee</option> <option <?=($rows['feeornofee'] == 'No Fees') ? 'selected="selected"' : ''?>>No Fees</option> </select></td> </tr> <tr> <td><div align="right">Lease Type</div></td> <td>:</td> <td colspan="3" style="text-align: left"><label for="lease"></label> <select name="lease" id="lease"> <option <?=($rows['lease'] == 'Rent Stabilized ') ? 'selected="selected"' : ''?>>Rent Stabilized </option> <option <?=($rows['lease'] == 'Prime, non-stabilized lease') ? 'selected="selected"' : ''?>>Prime, non-stabilized lease</option> <option <?=($rows['lease'] == 'Coop (sub) Lease') ? 'selected="selected"' : ''?>>Coop (sub) Lease</option> <option <?=($rows['lease'] == 'Condo Lease') ? 'selected="selected"' : ''?>>Condo Lease</option> <option <?=($rows['lease'] == 'Commercial Lease') ? 'selected="selected"' : ''?>>Commercial Lease</option> <option <?=($rows['lease'] == 'Other') ? 'selected="selected"' : ''?>>Other</option> </select></td> </tr> > <tr> <td><div align="right">Contact's Name</div></td> <td>:</td> <td colspan="3" style="text-align: left"><label for="contact"></label> <input name="contact" type="text" value="<? echo $rows['contact']; ?>" id="contact" size="40" maxlength="40" /></td> </tr> <tr> <td><div align="right">Name of Office</div></td> <td>:</td> <td colspan="3" style="text-align: left"><label for="office"></label> <input name="office" type="text" value="<? echo $rows['office']; ?>" id="office" size="40" maxlength="40" /></td> </tr> <tr> <td><div align="right">Pets</div></td> <td>:</td> <td colspan="3" style="text-align: left"><label for="pets"></label> <select name="pets" id="pets"> <option <?=($rows['pets'] == 'Pets Allowed') ? 'selected="selected"' : ''?>>Pets Allowed</option> <option <?=($rows['pets'] == 'No Pets') ? 'selected="selected"' : ''?>>No Pets</option> <option <?=($rows['pets'] == 'Pets Allowed - Small pets') ? 'selected="selected"' : ''?>>Pets Allowed - Small pets</option> </select></td> </tr> <tr> <td><div align="right">Phone<br /> </div> <br /></td> <td><div align="center">:<br /> <br /> </div></td> <td colspan="3" style="text-align: left"><label for="phone"></label> <span id="rental_phone"> <input type="text" name="phone" value="<? echo $rows['phone']; ?>"id="phone" /> <span class="textfieldMaxCharsMsg">Exceeded maximum number of characters.</span></span><br /> <span style="font-style: italic; font-size: 14px; font-weight: bold;">ex. (123) 456-7890 - Please keep this format.</span></td> </tr> <tr> <td style="text-align: right"><p>Location<br /> </p> <p><br /> <br /> <br /> </p></td> <td style="text-align: center">:<br /> <br /> <br /> <br /> <br /></td> <td colspan="3" style="text-align: left"><label for="cross_streets"></label> <input name="cross_streets" type="text" value="<? echo $rows['cross_streets']; ?>" id="cross_streets" size="80" maxlength="100" /> <br /> <span style="color: #F00">Please insert here the intersecting streets. Make sure you add the word "and" <br /> between streets <br /> OR<br /> You can put the property's full address. <br /> e.g. 193rd Street AND Jamaica Avenue, Jamaica, NY </span></td> </tr> <tr> <td style="text-align: right">Zip Code</td> <td style="text-align: center">:</td> <td colspan="3" style="text-align: left"><input name="zipcode" type="text" id="zipcode" value="<? echo $rows['zipcode']; ?>" size="9" maxlength="5" /></td> </tr> <tr> <td style="text-align: right"><span style="color: #000">Email </span><br /> <br /></td> <td style="text-align: center">:<br /> <br /></td> <td colspan="3" style="text-align: left"><label for="email"></label> <input name="email" type="text" value="<? echo $rows['email']; ?>" id="email" size="60" maxlength="60" /> <br /> <span style="font-size: 12px; font-style: italic;">This field is optional. It will be viewable to users.</span></td> </tr> <tr> <td><div align="right">Rooms</div></td> <td><div align="center">:</div></td> <td colspan="3" style="text-align: left"><label for="rooms"></label> <select name="rooms" id="rooms"> <option <?=($rows['rooms'] == '0') ? 'selected="selected"' : ''?>>0</option> <option <?=($rows['rooms'] == '1') ? 'selected="selected"' : ''?>>1</option> <option <?=($rows['rooms'] == '1.5') ? 'selected="selected"' : ''?>>1.5</option> <option <?=($rows['rooms'] == '2') ? 'selected="selected"' : ''?>>2</option> <option <?=($rows['rooms'] == '2.5') ? 'selected="selected"' : ''?>>2.5</option> <option <?=($rows['rooms'] == '3') ? 'selected="selected"' : ''?>>3</option> <option <?=($rows['rooms'] == '3.5') ? 'selected="selected"' : ''?>>3.5</option> <option <?=($rows['rooms'] == '4') ? 'selected="selected"' : ''?>>4</option> <option <?=($rows['rooms'] == '4.5') ? 'selected="selected"' : ''?>>4.5</option> <option <?=($rows['rooms'] == '5') ? 'selected="selected"' : ''?>>5</option> <option <?=($rows['rooms'] == '5.5') ? 'selected="selected"' : ''?>>5.5</option> <option <?=($rows['rooms'] == '6') ? 'selected="selected"' : ''?>>6</option> </select> 0 = Studio, 1 = 1 Bedroom, etc.</td> </tr> <tr> <td><div align="right">Bath</div></td> <td><div align="center">:</div></td> <td colspan="3" style="text-align: left"><label for="bath"></label> <select name="bath" id="bath"> <option <?=($rows['bath'] == '0') ? 'selected="selected"' : ''?>>0</option> <option <?=($rows['bath'] == '1') ? 'selected="selected"' : ''?>>1</option> <option <?=($rows['bath'] == '1.5') ? 'selected="selected"' : ''?>>1.5</option> <option <?=($rows['bath'] == '2') ? 'selected="selected"' : ''?>>2</option> <option <?=($rows['bath'] == '2.5') ? 'selected="selected"' : ''?>>2.5</option> <option <?=($rows['bath'] == '3') ? 'selected="selected"' : ''?>>3</option> <option <?=($rows['bath'] == '3.5') ? 'selected="selected"' : ''?>>3.5</option> <option <?=($rows['bath'] == '4') ? 'selected="selected"' : ''?>>4</option> </select></td> </tr> <tr> <td><div align="right">Square ft.</div></td> <td><div align="center">:</div></td> <td colspan="3" style="text-align: left"><label for="square"></label> <input name="square" type="text" value="<? echo $rows['square']; ?>" id="square" size="6" maxlength="6" /></td> </tr> <tr> <td><div align="right"><span style="color: #F00">*</span>Rent</div></td> <td><div align="center">:</div></td> <td colspan="3" style="text-align: left"><label for="rent"> $ <input name="rent" type="text" value="<? echo $rows['rent']; ?>" id="rent" size="10" maxlength="10" /> </label></td> </tr> <tr> <td><div align="right"><span style="color: #F00">*</span>Fees </div></td> <td><div align="center"> <p>: </p> </div></td> <td colspan="3" style="text-align: left"><label for="fees"></label> <span id="sprytextfield3"> <input name="fees" type="text" id="fees" value="<? echo $rows['fees']; ?>" size="90" /> </span></td> </tr> <tr> <td colspan="5" style="text-align: center; font-weight: bold; font-size: 18px; color: #F00;"> Video Link</td> </tr> <tr> <td><div align="right">URL</div></td> <td><div align="center">:</div></td> <td colspan="3"><p> </p> <p> <input name="url" type="text" id="url" value="<? echo $rows['url']; ?>"size="80" /> </td> </tr> <tr> <td><div align="center" style="color: #F00"> <div align="right">Name Your Video Link</div> </div></td> <td><div align="center"> <p>: </p> </div></td> <td colspan="3"><br /> <p> <input name="videotitle" type="text" id="videotitle" value="<? echo $rows['videotitle']; ?>"size="80" /> <br /> <span style="color: #E62E00">If you posted a URL for your video you must give it a title to give it a link.</span></p></td> </tr> <tr> <td colspan="2" style="text-align: center"> </td> <td style="text-align: center"> </td> <td style="text-align: center"> </td> <td style="text-align: center"> </td> </tr> <tr> <td colspan="2" style="text-align: center"> </td> <td style="text-align: center"> </td> <td style="text-align: center"> </td> <td style="text-align: center"> </td> </tr> <tr> <td colspan="2" style="text-align: center"><img src="<? echo $rows['imageurl1']; ?>" width="200" /><br /></td> <td width="213" style="text-align: center"><img src="<? echo $rows['imageurl2']; ?>" width="200" /></td> <td width="180" style="text-align: center"><img src="<? echo $rows['imageurl3']; ?>" width="200" /></td> <td width="188" style="text-align: center"><img src="<? echo $rows['imageurl4']; ?>" width="200" /></td> </tr> <tr> <td colspan="2" style="text-align: center"><div align="center"> <input id="image3" type="file" name="image1" /> </div></td> <td style="text-align: center"><div align="center"> <input id="image3" type="file" name="image2" /> </div></td> <td style="text-align: center"><div align="center"> <input id="image3" type="file" name="image3" /> </div></td> <td style="text-align: center"><div align="center"> <input id="image4" type="file" name="image4" /> </div></td> </tr> <tr> <td colspan="5" style="text-align: center"> </td> </tr> <tr> <td colspan="2" style="text-align: center"><img src="<? echo $rows['imageurl5']; ?>" width="200" /></td> <td style="text-align: center"><img src="<? echo $rows['imageurl6']; ?>" width="200" /></td> <td style="text-align: center"><img src="<? echo $rows['imageurl7']; ?>" width="200" /></td> <td style="text-align: center"><img src="<? echo $rows['imageurl8']; ?>" width="200" /></td> </tr> <tr> <td colspan="2" style="text-align: center"><input id="image5" type="file" name="image5" /></td> <td style="text-align: center"><input id="image6" type="file" name="image6" /></td> <td style="text-align: center"><input id="image7" type="file" name="image7" /></td> <td style="text-align: center"><input id="image8" type="file" name="image8" /></td> </tr> <tr> <td colspan="2" style="text-align: center"><img src="<? echo $rows['imageurl9']; ?>" width="200" /></td> <td style="text-align: center"><img src="<? echo $rows['imageurl10']; ?>" width="200" /></td> <td style="text-align: center"><img src="<? echo $rows['imageurl11']; ?>" width="200" /></td> <td style="text-align: center"><img src="<? echo $rows['imageurl12']; ?>" width="200" /></td> </tr> <tr> <td colspan="2" style="text-align: center"><input id="image9" type="file" name="image9" /></td> <td style="text-align: center"><input id="image10" type="file" name="image10" /></td> <td style="text-align: center"><input id="image11" type="file" name="image11" /></td> <td style="text-align: center"><input id="image12" type="file" name="image12" /></td> </tr> <tr> <td colspan="5" style="text-align: center"> </td> </tr> <tr> <td colspan="5" style="text-align: center"><span style="color: #F00">*</span> REQUIRED FIELDS </td> </tr> <tr> <td colspan="5" style="text-align: center"> PRESS SUBMIT ONCE!!!</td> </tr> <tr> <td colspan="5" style="text-align: center"><input type="submit" name="button" id="button" value="Submit Changes" /> <input type="reset" name="button" id="button" value="Reset" /></td> </tr> </table> </form> </body> </html> The Script Code: [Select] <?php session_start(); include('SimpleImage.php'); $image = new SimpleImage(); //error_reporting(E_ALL); // image upload folder $image_folder = 'images/classified/'; // fieldnames in form $all_file_fields = array('image1', 'image2' ,'image3', 'image4', 'image5', 'image6', 'image7', 'image8', 'image9', 'image10', 'image11', 'image12'); // allowed filetypes $file_types = array('jpg','gif','png'); // max filesize 5mb $max_size = 5000000; //echo'<pre>';print_r($_FILES);exit; $time = time(); $count = 1; foreach($all_file_fields as $fieldname){ if($_FILES[$fieldname]['name'] != ''){ $type = substr($_FILES[$fieldname]['name'], -3, 3); // check filetype if(in_array(strtolower($type), $file_types)){ //check filesize if($_FILES[$fieldname]['size']>$max_size){ $error = "File too big. Max filesize is ".$max_size." MB"; }else{ // new filename $filename = str_replace(' ','',$myusername).'_'.$time.'_'.$count.'.'.$type; // move/upload file $image->load($_FILES[$fieldname]['tmp_name']); if($image->getWidth() > 150) { //if the image is larger that 150. $image->resizeToWidth(500); //resize to 500. } $target_path = $image_folder.basename($filename); //image path. $image->save($target_path); //save image to a directory. //save array with filenames $images[$count] = $image_folder.$filename; $count = $count+1; }//end if }else{ $error = "Please use jpg, gif, png files"; }//end if }//end if }//end foreach if($error != ''){ echo $error; }else{ //error_reporting(E_ALL); //ini_set('display_errors','On'); $id = $_POST['id']; $id = substr($id, 0,5); if($id < 1 || $id > 99999) exit; $servername = "localhost"; $username = ""; $password = ""; if(!$_POST["title"] || !$_POST["rent"] || !$_POST["fees"]){ header('location: fields.php'); }else if (!(preg_match('#^\d+(\.(\d{2}))?$#',($_POST["rent"])))){ header('location: rent.php'); }else{ $conn = mysql_connect($servername,$username,$password)or die(mysql_error()); mysql_select_db("genesis_apts",$conn); // validate id belongs to user $sql_check = "SELECT * FROM apartments WHERE id = '".$id."' AND username = '".$myusername."'"; $res = mysql_query($sql_check,$conn) or die(mysql_error()); $count = mysql_num_rows($res); if ($count > 0){ $sql = "UPDATE apartments SET title = '".mysql_real_escape_string($_POST['title'])."', description = '".mysql_real_escape_string($_POST['description'])."', cross_streets = '".mysql_real_escape_string($_POST['cross_streets'])."', county = '".mysql_real_escape_string($_POST['county'])."', town = '".mysql_real_escape_string($_POST['town'])."', service = '".mysql_real_escape_string($_POST['service'])."', phone = '".mysql_real_escape_string($_POST['phone'])."', contact = '".mysql_real_escape_string($_POST['contact'])."', office = '".mysql_real_escape_string($_POST['office'])."', pets = '".mysql_real_escape_string($_POST['pets'])."', email = '".mysql_real_escape_string($_POST['email'])."', rooms = '".mysql_real_escape_string($_POST['rooms'])."', bath = '".mysql_real_escape_string($_POST['bath'])."', square = '".mysql_real_escape_string($_POST['square'])."', rent = '".mysql_real_escape_string($_POST['rent'])."', fees = '".mysql_real_escape_string($_POST['fees'])."', service = '".mysql_real_escape_string($_POST['service'])."', feeornofee = '".mysql_real_escape_string($_POST['feeornofee'])."', lease = '".mysql_real_escape_string($_POST['lease'])."', url = '".mysql_real_escape_string($_POST['url'])."', zipcode = '".mysql_real_escape_string($_POST['zipcode'])."', videotitle = '".mysql_real_escape_string($_POST['videotitle'])."', imageurl1 = '".mysql_real_escape_string($images[1])."', imageurl2 = '".mysql_real_escape_string($images[2])."', imageurl3 = '".mysql_real_escape_string($images[3])."', imageurl4 ='".mysql_real_escape_string($images[4])."', imageurl5 = '".mysql_real_escape_string($images[5])."', imageurl6 = '".mysql_real_escape_string($images[6])."', imageurl7 = '".mysql_real_escape_string($images[7])."', imageurl8 = '".mysql_real_escape_string($images[8])."', imageurl9 = '".mysql_real_escape_string($images[9])."', imageurl10 = '".mysql_real_escape_string($images[10])."', imageurl11 = '".mysql_real_escape_string($images[11])."', imageurl12 = '".mysql_real_escape_string($images[12])."' WHERE id = '".$id."'"; //replace info with the table name above $result = mysql_query($sql,$conn) or die(mysql_error()); header('location: apartments.php'); }else{ header('location: wrong.php'); } } } ?> Here is my code Code: [Select] if (isset($_GET['hide'])){ $id = intval($_GET['hide']); $explode = explode(",", $_COOKIE['hide']); if (in_array($id, $explode)) { } setcookie('hide', ''.$id.'',time()+32000000); header('Location: index.php'); exit; } My Explanation: If somone clicks ?hide=2 It will add the cookie hide, and the value to 2. Now, if somone clicks ?hide=1, I need it to add 2,1 how is that possible? Then if the in_array is correct, (Means if the value of the cookie is 2 and they clicked ?hide=2) how do I take that number "2" out of the it (Remove it) and make a new array that will be inserted to my $id variable? Hello, how could I do a replace using one array? Example: Code: [Select] $changes = array( "{NAME}" => $name, "{CHANGED_PART}" => $desc, "{CHANGE_D}" => date("j"), "{CHANGE_M}" => date("n") ); And then I would want to replace all parts in the left with parts in the right inside a string.Is that possible ? thank you.. The output of the array1 is looks as below. Code: [Select] Array ( [num_found] => 60 [articles] => Array ( [0] => Array ( [aut] => Array ( [0] => ANI ) [by] => ANI [Id] => 278 [key] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hydrogenated ) [norm] => true [body] => Gorging [cat] => Array ( [0] => food & drink ) [pm] => ANI|P#M#D| [pm] => Beware! [seo] => junk [pmT] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hydrogenated ) [pid] => 72 [Date] => 2011-06-02T00:00:00Z [section] => Health [sd] => 3506926 [ti] => Beware! Junk food can cause miscarriage [ur] => riage [kick] => [subtitle] => [image] => [Tag] => ) [1] => Array ( [aut] => Array ( [0] => URVASHI ASHER ) [by] => URVASHI [Id] => 277 [key] => Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [norm] => true [body] => division. [cat] => Array ( [0] => entertainment ) [pm] => URVASHI ASHER|P#M#D|TNN [pm] => booked [seo] => film [pmT] => Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [pid] => 72 [Date] => 2011-06-02T00:00:00Z [section] => News & Interviews [sd] => 3921796 [ti] => Amitabh booked [uri] => film [kick] => [subtitle] => [image] => [Tag] => ) I have a array2 similar to this. Code: [Select] Array ( [num_found] => 70 [articles] => Array ( [0] => Array ( [aut] => Array ( [0] => ANI ) [by] => RNI [Id] => 643 [key] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hy ) [norm] => true [body] => Test [cat] => Array ( [0] => drink ) [pm] => ANI|P#M#D| [pm] => Beware! [seo] => junk [pmT] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hydrogenated ) [pid] => 73 [Date] => 2011-06-02T00:00:00Z [section] => Health [sd] => 8434 [ti] => Beware [ur] => riage [kick] => [subtitle] => [image] => [Tag] => ) [1] => Array ( [aut] => Array ( [0] => URVASHI ASHER ) [by] => URVASHI [Id] => 345 [key] => Array ( [0] => film [1] => design [2] => biography [3] => bachchan [4] => cinema ) [norm] => true [body] => division. [cat] => Array ( [0] => entertainment ) [pm] => URVASHI|P#M#D|TNN [pm] => booked [seo] => film [pmT] => Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [pid] => 72 [Date] => 2011-06-02T00:00:00Z [section] => News & Interviews [sd] => 43543543 [ti] => booked [uri] => film [kick] => [subtitle] => [image] => [Tag] => ) Please tell me how to add the 1st value from array2 and replace in array1. The output should look like Code: [Select] Array ( [num_found] => 60 [articles] => Array ( [0] => Array ( [aut] => Array ( [0] => ANI ) [by] => RNI [Id] => 643 [key] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hy ) [norm] => true [body] => Test [cat] => Array ( [0] => drink ) [pm] => ANI|P#M#D| [pm] => Beware! [seo] => junk [pmT] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hydrogenated ) [pid] => 73 [Date] => 2011-06-02T00:00:00Z [section] => Health [sd] => 8434 [ti] => Beware [ur] => riage [kick] => [subtitle] => [image] => [Tag] => ) [1] => Array ( [aut] => Array ( [0] => URVASHI ASHER ) [by] => URVASHI [Id] => 277 [key] => Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [norm] => true [body] => division. [cat] => Array ( [0] => entertainment ) [pm] => URVASHI ASHER|P#M#D|TNN [pm] => booked [seo] => film [pmT] => Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [pid] => 72 [Date] => 2011-06-02T00:00:00Z [section] => News & Interviews [sd] => 3921796 [ti] => Amitabh booked [uri] => film [kick] => [subtitle] => [image] => [Tag] => ) where array[0] is the value from array2. hi, im trying to replace an arrays key with its value. this is so i can display the html on the pages. eg: index.html <title>{BOARD_TITLE}</title> index.php $template->replace(array( '{BOARD_TITLE}' => $settings['board_name'] )); template.php(template class) function replace($array) { foreach ($array as $key => $val) { echo $key = $val; } return; } however {BOARD_TITLE} does not get replaced with the value. where am i going wrong? Thanks Hey i hope the title explains it enough. I'm just trying to get into classes. I am making a function to make making mysql querys easier. I pass the class the first string $db->inputstring("INSERT INTO ? (?,?) VALUES ('?','?')"); $db->format('mytable','id','name','1','ben'); Can someone tell me how the format fuction would look? Im not sure how to swap arrays. Hi, If i had the following array: $test = array('key1'=>'test1', 'key2'=>'value2', 'key3'=>array('test','test1','test2')); Is there a way i could change all the values that have the word "test" to "hello"? So basically end up with the following array: $test = array('key1'=>'hello1', 'key2'=>'value2', 'key3'=>array('hello','hello1','hello2')); Is this possible? Thanks Hello, I am a newbie PHP hobbyist. I mess with php strictly for my own amusement. Up until now I have been able to Google my way to a answer to my questions but I'm lost on this one. I'm not really sure what to even search for. I have a few different mysql queries that returns the following arrays. qid15=37,37,37,37,37,37,37,37,37,37,37,38,38,39,39,41 qid17=47,47,47,47,47,48,49,49,49,50,50,50,51,51 qid18=52,52,52,52,52,52,52,52,54,54,55,55,55,56,56,56 qid19=57,57,57,57,58,59,59,61,61 What I would like to do is replace or create a new array for each result with the lowest value as 1 the next lowest as 2 then 3 and 4. My new array would look something like this oldqid15=37,37,37,37,37,37,38,38,39,39,41 newqid15=1,1,1,1,1,1,2,2,3,3,4 I sure would appreciate any ideas on this. I'm thinking there is probably a simple solution and before I waste anymore time I'll stop and ask for directions(as my wife would suggest). I know you can use str_replace to replace a value with another value, or an array of values with an array of other values. What I'd like to do is replace each instance of a single value with an array of values which get iterated through for each instance found. Think prepared statements that you don't have to call setString or setInt on. Here's a made up example. Code: [Select] $db->query = 'SELECT * FROM users WHERE user_name = ? AND user_role = ?; $db->params = array('hopelessX','admin'); $db->executeQuery(); //this contains a call to a private function that does the replace I have everything else working except this. It works if I specify an array of values to replace and do something like below, but I was hoping to not have to support that method of replace if I didn't need to. Code: [Select] $db->query = 'SELECT * FROM users WHERE user_name = :name AND user_role = :role; $db->replace = array(':name',':role'); $db->params = array('hopelessX','admin'); $db->executeQuery(); //this contains a call to a private function that does the replace I can do it that way if I need to or I could make some overly complicated thing that finds each instance of the question mark, replaces it with a unique value and then adds that unique value to a replace array similarly to the method in code block two but that defeats to the purpose of making this simple database adapter in the first place (although it was mostly just for fun anyway). Anyway, if anyone has any advice and knows of a simple means to achieve this goal that would be much appreciated. Thanks in advance Hi Guys, What am I missing here Code: [Select] $lside_content2 .= "<tr><td> <a class=\"bl\" href = \"quiz_int3.php?id=$row[id]&pagenum=$pagenum\" target='_self'> $row[title]</a> ". echo ThumbsUp::item($row['id']); ."</p></td></tr>"; } I'm getting this error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING thank you for your help! Hi all, hope some can help because this is giving me more grey hairs than I allready have.
I am trying to solve the following: I'm pulling in JSON data, decoding it to a array which I write to a database. However in the array are social security numbers that I don't want to store (privacyregulations). So I need to check if a row has a social security number and if it has one I want to replace it with 'anonymized'. I have got it to work successfully on a local Xampp test environment using the following script (extract off course): //first anonymize the social security number (ssn) if exists $data = json_decode($jsondata, true);
// then fill the tables foreach($data as $row) {
$insertstatement = mysqli_query($link,"INSERT INTO $tablename (personid, type, ssn) VALUES ('".$row["personid"]."', '".$row["type"]."', '".$row["ssn"]."') ON DUPLICATE KEY UPDATE type = VALUES(type), bsn = VALUES(ssn)");
This leads to a filled table where the ssn's are filled with 'anonymized' if they exist in the array and empty if it does not exist for that row. This is exactly what I want ;-).
Personid type ssn
However, when I run the same script on a Lamp production environment (lamp stack on an Azure server) it does not work. Then the scripts results in totally ignoring the whole row that contains a ssn??
Personid type ssn Hope someone can help to get this working on my production environment as well.
Richard
Parse error: syntax error, unexpected '(', expecting T_STRING or T_VARIABLE or T_NUM_STRING... I'm getting this error for this line: if ((int)$row['loyalty']<20) Thanks in advanced. I need a help in the following : I have an admin page from where the admin attaches an gif image.This attached image should be shown to the user as a scrolling image. The code for the scrolling image is done through javascript. So my actual problem is getting the name of the attached gif image to the javascript array which is used for scrolling horizontally. Hi there i have an array that catches all appropriate records from my database and selects a random row which is then outputted as a hyperlink. Code: [Select] $colname_Class3 = "3"; $colname_Ships3 = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_Ships3 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query_Ships3 = sprintf("SELECT * FROM ships WHERE PlayerName = %s AND Class=%s AND Template='1'", GetSQLValueString($colname_Ships3, "text"), GetSQLValueString($colname_Class3, "int")); $Ships3 = mysql_query($query_Ships3, $swb) or die(mysql_error()); $totalRows_Ships3 = mysql_num_rows($Ships3); $count = 0; while ($row = mysql_fetch_assoc($Ships3)){ $array[$count]['ShipID'] = $row['ShipID']; $array[$count]['ShipName'] = $row['ShipName']; $array[$count]['Class'] = $row['Class']; $count++; } $total3 = count($array3); $random3 = rand(0,$total3 - 1); $random_ship3 = $array3[$random3]; $ShipID3 = $random_ship3['ShipID']; $url3 = '<a href="add_ship.php?recordID='.$ShipID3.'">Test3</a>'; echo '<span style="color: #009900">'; echo $url3;} This all worked fine until i added another similar query: Code: [Select] $colname_Class4 = "4"; $colname_Ships4 = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_Ships4 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query_Ships4 = sprintf("SELECT * FROM ships WHERE PlayerName = %s AND Class=%s AND Template='1'", GetSQLValueString($colname_Ships4, "text"), GetSQLValueString($colname_Class4, "int")); $Ships4 = mysql_query($query_Ships4, $swb) or die(mysql_error()); $totalRows_Ships4 = mysql_num_rows($Ships4); $count = 0; while ($row = mysql_fetch_assoc($Ships4)){ $array[$count]['ShipID'] = $row['ShipID']; $array[$count]['ShipName'] = $row['ShipName']; $array[$count]['Class'] = $row['Class']; $count++; } $total4 = count($array4); $random4 = rand(0,$total4 - 1); $random_ship4 = $array4[$random4]; $ShipID4 = $random_ship4['ShipID']; $url4 = '<a href="add_ship.php?recordID='.$ShipID4.'">Test4</a>'; echo $url4;} On first look it seems fine, however once the page is refreshed the parsed recordID's both become the same number, e.g: Test3 recordID 8 Test4 recordID 24 then after refreshed: Test3 recordID 24 Test4 recordID 24 Is there anything wrong with my code?? Thanks What I'm trying to achieve is to get a set of data from a database and then cycle through the array and add the contents of each row to a variable. I'm trying to use $i to cycle through the array but at the moment it's just printing the first character of each set. Any help would be great. $fav_products_sql = "select fav_products.product_id, fav_products.product_order, product_list.product_name from fav_products, product_list where fav_products.product_id = product_list.id"; $fav_products_rs = mysql_query($fav_products_sql, $conn); $fav_products = mysql_fetch_array ($fav_products_rs); while ($i < 11) { $_SESSION['fav_products'] = "<li>" . $fav_products['product_order']['$i'] . ": " . $fav_products['product_name']['$i'] . "</li>"; $i++; } I'm having trouble getting all of the results out of a query array, as it is I only get the first result and nothing else. Here's the basic code I'm working with: Code: [Select] $query = "SELECT data_txt FROM jos_servicedirectory_fields_data WHERE fieldid = 19 AND itemid = $item->itemid"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $listtags = $row['data_txt']; $tags = "$listtags, "; $title = "<div class='servicedirectoryItemTitle'><table class=\"sdlistingitemtitle\" cellspacing=\"5\"><tbody><tr>$listingimage<td style=\"vertical-align:top;width:690px;\">$listingbasicicon<span class=\"$listingtitleclass\">$listinglogo<a href=\"{$href}\" {$onClick} title=\"{$item->title}\">{$item->title}</a></span><br /><span class=\"listingdescription\">$listingdescription</span></td><td style=\"vertical-align:top;\">$featuredribbon$moreinfobasic</td></tr></tbody></table><div class=\"listingbottom\">Tags:<span class=\"listingtags\"> $tags </span></div></div>"; } I've also tried using a foreach loop thinking that would pop all of the results but I end up not getting anything at all then. I'm guessing I'm setting the foreach loop up wrong. Here's how I'm trying to do it: Code: [Select] foreach($listtags as $value) { $tags = $value; } Does it have something to do with sticking the $tags variable in the $title variable? I wouldn't think that would matter, but the strange thing is I use this exact same query in a different part of this component and just echo it directly and it works fine. //First I'm assigning a $variable ($emailzipmatch) to query a database table called(repzipcodes) and having it pull and display 1 to 3 records based on matching up a customer's zip code (RepZipCode = $CustomerZipMatch) with 1 to 3 other people (GROUP BY RepId HAVING COUNT(1) <= 3") that want that customer's information from that particular zip code. <This code works fine> // CODE WORKS BELOW Code: [Select] <?php $emailzipmatch = mysql_query("SELECT * FROM repzipcodes WHERE RepZipCode = $CustomerZipMatch GROUP BY RepId HAVING COUNT(1) <= 3") or die(mysql_error()); $recipients = array(); while($row = mysql_fetch_array($emailzipmatch)) { $recipients[] = $row['RepEmail']; echo "Agent's Email Address: "; echo 'font color="#FF7600"',$row['RepEmail'], '/font'; echo '<br />'; echo "Rep's ID: "; echo '<br />'; echo 'font color="#FF7600"',$row['RepId'], '/font'; echo '<br />'; echo 'hr align="left" width="50%" size="2" /'; } //MY PROBLEM BELOW // For the NEXT step of the process above I would take $row['RepEmail'] and $row['RepId'] which can have 1 to 3 results and assign the 1 to 3 results a new $variable so it can be inserted into a different db table so I can track the results of the query ($emailzipmatch = ) from the top of the page: ie.. <New Variable> <Listed from above> $SentRepId 0 = RepId (results from above echo area) $SentRepId 1 = RepId (results from above echo area) $SentRepId 2 = RepId (results from above echo area) // Below I'd like to insert the above results into a new database $?Variable??? = mysql_query("INSERT INTO sentemail (SentRepId0, SentRepId1, SentRepId2,SentDateTime ) VALUES ( '$_SESSION[RepId]', // ????? '$_SESSION[RepId]', // ????? '$_SESSION[RepId]', // ????? NOW() )") or die(mysql_error()); Thank ahead of time for any help you guys can give me. Please respond with ANY question if my coding or request isn't clear or if I've been confusing due to my lack of experience with PHP and MySQL. MOD EDIT: code tags added. How do I access objects inside an array of database results? I have a method that returns MySQL results as an array "$projects". Running print_r($projects) gives me this: array(1) ( "projects" => object Database_MySQL_Result(6) { protected _internal_row => integer 0 protected _query => string(875) "SELECT [skipping remainder of long query]" protected _result => resource(mysql result) protected _total_rows => integer 53 protected _current_row => integer 0 protected _as_object => string(13) "Model_Project" } ) If I do this: foreach($projects as $project) { echo $project->PROJECT_NAME; } I get "Undefined property: Database_MySQL_Result::$PROJECT_NAME" If I do this: foreach($projects as $project) { echo $project[0]; } the browser will display projects.id for the first returned row *only* echo $project[1] returns the project.id for the second row only. And so on. Each returned row contains over a dozen cells. It's almost as if I'm referring to the array incorrectly, or referring to the wrong array. I feel as though I'm making a very simple (and perhaps dumb) mistake here, but can't quite figure out what it is. Thanks. |