PHP - Getting A Certain Part Of An Array
Hi all,
I have the following : Code: [Select] print_r($fqlResult); Which gives the following result : Code: [Select] Array ( [0] => Array ( [src_big] => http://a5.sphotos.ak.fbcdn.net/hphotos-ak-ash4/299117_2121699756933_1079004376_2442101_3103384_n.jpg ) ) I want to grab just the image url from it and store it in a seperate variable. Any ideas how I can do this plzzz ? Thanks in advance, Scott. Similar TutorialsMy query gets the results and orders by one of the fields. Once I get the MySQL results I would like to find the first entry that has a letter as the first character of the same field that the list was ordered by, then split the results in to two parts and swap them. So that the results that have a letter at the start of the same sorted field are as the begining and the results that have the numbers as the start at the end of the array. But also so that the array works the same way as the original results string, so i can use say $results['mysqlfield'] So, I am making a system that shows if there are new posts or not. How I have it done is a singular array called "Read" will have all of the IDs of the read posts. It is entered in the database like so: $ID2= $_SESSION['UserID']; $ID2 = mysql_real_escape_string($ID2); $Readornot=mysql_query("SELECT * FROM forum_read WHERE Reader_ID='$ID2'"); $Readornot2=mysql_fetch_array($Readornot); if($ID2==0) { } else { if(empty($Readornot2)) { $Read1= array("Read" => $Get_TopicID); $Read2 = implode('-',$Read1); mysql_query("INSERT INTO forum_read (Reader_ID,`Read`) values ('$ID2','$Read2')") or die(mysql_error()); } else { $cr_topicid=$Readornot2['Read']; if(strpos($cr_topicid,'-')) { $cr_topicid=explode('-',$cr_topicid); if(!in_array($Get_TopicID,$cr_topicid,true)) { $Read0=implode('-',$cr_topicid); $Read1= array("Read" => $Read0, $Get_TopicID); $Read2= implode('-',$Read1); mysql_query("UPDATE forum_read SET`Read`='$Read2' WHERE Reader_ID='$ID2'") or die(mysql_error()); } } else { if($cr_topicid==$Get_TopicID) { } else { $Read1= array("Read" => $cr_topicid, $Get_TopicID); $Read2= implode('-',$Read1); mysql_query("UPDATE forum_read SET`Read`='$Read2' WHERE Reader_ID='$ID2'") or die(mysql_error()); } } } } It all works fine and dandy to the point where if it's in the database an icon will be black and white, and if its not it wont, signifying read or unread. What I need to do, however, is make it so when there is a new post, it takes all of the entries in the database with the topic ID out of the database, and strips the one part. What I thought of at first was to use str_replace to replace "-" . $TopicID . "-" with "-" But of course, that won't work, because the very first entry could be the topic ID, so it would just be $TopicID- and not -$TopicID- in the database. So, how would I go about doing this? I have an array like this
$rows = array( array( 'fruit.name' => 'Apple', 'fruit.colour' => 'Red', 'fruit.weight' => '0.1', 'vegetable.name' => 'Carrot', 'vegetable.colour' => 'Orange', 'vegetable.weight' => '0.05' ), array( 'fruit.name' => 'Banana', 'fruit.colour' => 'Yellow', 'fruit.weight' => '0.7', 'vegetable.name' => 'Potato', 'vegetable.colour' => 'Brown', 'vegetable.weight' => '0.6' ) ); Hello, I have several job categories on a recruitment website: $job_category[0]="accountant"; $job_category[1]="php programming"; $job_category[2]="baseball"; these job categories are contained within the urls imediately after the recruiting company's name and before the word "jobs", so we could have the following urls: www.url.com/company_name_accountant_jobs www.url.com/another_company_name_baseball_jobs www.url.com/yet_another_company_name_php programming_jobs Using the array $job_categories and the page path after the foward slash (e.g. yet_another_company_name_php programming_jobs) please could you tell me how to strip out the job category and the company name? (I'll extract the page path using a mod_rewrite) Thanks Stu I wish to get only the last part of each array 'g' And then show non duplicated 'g' but in the same order that it was in the original array. Code: [Select] <?php $formarray = array( 'name' => array( 'i1'=> array('a'=>'a1', 'b'=>'b1', 'c'=>'c1', 'd'=>'d1', 'e'=>'e1', 'f'=>'f1', 'g'=>'g1'), 'i2'=> array('a'=>'a2', 'b'=>'b2', 'c'=>'c2', 'd'=>'d2', 'e'=>'e2', 'f'=>'f2', 'g'=>'g2'), 'i3'=> array('a'=>'a3', 'b'=>'b3', 'c'=>'c3', 'd'=>'d3', 'e'=>'e3', 'f'=>'f3', 'g'=>'g3'), 'i4'=> array('a'=>'a4', 'b'=>'b4', 'c'=>'c4', 'd'=>'d4', 'e'=>'e4', 'f'=>'f4', 'g'=>'g4'), ) ); foreach ($formarray as $newarray => $a) { ?><strong><?=$newarray;?></strong><br><? foreach ($a as $key => $k) { ?>"<?=$key;?>", <? foreach ($k as $b) { //if ($k['DBfield'] != "") { ?>"<?=$b;?>", <? //} } ?><br><? } //end of second foreach ?><br><br><br><? } //end of first foreach ?> Hello, I'm trying to store the match I get in $result to a variable so that I can easily compare its other values against other values. Here is my code.. //loop through new orders array foreach ( $orders as $order ) { //compare job number and line item against database information for a match if ( array_search ( $order[ 'job_number' ], array_column ( $result, 'job_number' ) ) !== false && array_search ( $order[ 'line_item' ], array_column ( $result, 'line_item' ) ) !== false ) { $db_match = key($result); echo '<br><br>'.$db_match.'<br><br>'; echo 'Job: ' . $order['job_number'] . '/Line Item: ' . $order['line_item'] . ' was found<br>'; //new order which doesn't exist in database } else { echo 'Job: ' . $order['job_number'] . '/Line Item: ' . $order['line_item'] . ' was not found<br>'; } } When I do $db_match = key($result); I get 0 each time and when I do: $db_match = array_search ( $order[ 'job_number' ], array_column ( $result, 'job_number' ) ) !== false && array_search ( $order[ 'line_item' ], array_column ( $result, 'line_item' ) ) !== false; I get 1 each time...which I assume is the number of matches which meet that criteria instead of the array key I found a match on. Edited May 8, 2020 by mongoose00318hi, also i have an array with this values: $a=array('111-aa-zz-4','222-aa-zz-6','333-aa-zz-5'); and i want to sort(); this array, but it must ignore the numbers behind -aa-zz- also 4,6,5 how should i do this? best regards, tastro Code: [Select] <? $out = preg_replace('/^(.{701}[^.]*).*/i','$1.',$detrsltnewsrow[news_desc]); echo $out; ?> </td></tr><tr><td colspan="2" class="para" style="padding-left:10px;"> <?= substr(stripslashes(trim($detrsltnewsrow[news_desc])),701) ?> </td></tr> I have the above snippet.. The first php statement, basically grasp the first 701 characters with the closet next stop "." character and out puts it. then out puts the HTML tags I have a problem with the second statement. I want to output anything after what has been outputted by: Code: [Select] <? $out = preg_replace('/^(.{701}[^.]*).*/i','$1.',$detrsltnewsrow[news_desc]); echo $out; ?> So need the correct syntax for Code: [Select] <?= substr(stripslashes(trim($detrsltnewsrow[news_desc])),701) ?> Currently it breaks at exactly the 701 character, want it to continue from the sentence the first code ended in. I am having problems making it so that if I typed site.php?url=http://example.com it would auto get rid of the http://. Here's my code. $url="{$_GET['url']}"; str_replace('http://', '', $url); echo "<a href=\"http://$url\">$url</a>"; I opened a thread yesterday about an XSS vulnerability when the user is logged in. I'll summarize is in a short quote: Quote http://host/editText.php?fieldname=slogan&content=slogan<img src=x onerror=alert("XSS")> This vulnerability only works if the user is logged in. I want to secure it anyway to give the security companies contacting me about this a break. xyph solved my problem with this: Code: [Select] foreach( $_REQUEST as $key => $val ) $_REQUEST[$key] = htmlentities($val); He warned me it was a risky but I didn't take him that seriously. Well guess he was right. The foreach loop he gave me does protect me from the XSS attack, but it also disables the users to use any kind of code in the pages. Next time xyph warns me its risky, I'll know he means it. Now to my problem, how do I use this foreach loop without disabling the user of using simple html tags? Here's the file (editText.php) where the foreach loop was used: Code: [Select] <?php session_start(); // THE LOOP WAS USED HERE BUT I REMOVED IT DUE TO THE USERS PROBLEM. function getSlug( $page ) { $page = strip_tags( $page ); preg_match_all( "/([a-z0-9A-Z-_]+)/", $page, $matches ); $matches = array_map( "ucfirst", $matches[0] ); $slug = implode( "-", $matches ); return $slug; } $fieldname = $_REQUEST['fieldname']; $encrypt_pass = @file_get_contents("files/password"); if ($_COOKIE['wondercms']!=$encrypt_pass) { echo "You must login before using this function!"; exit; } $content = rtrim(stripslashes($_REQUEST['content'])); // if to only allow specified tags if($fieldname=="title") $content = strip_tags($content); else $content = strip_tags($content,"<audio><source><embed><iframe><p><h1><h2><h3><h4><h5><h6><a><img><u><i><em><strong><b><strike><center><pre>"); $content = trim($content); $content = nl2br($content); if(!$content) $content = "Please be sure to enter some content before saving. Just type anything in here."; $content = preg_replace ("/%u(....)/e", "conv('\\1')", $content); if($fieldname>0 && $fieldname<4) $fname = "attachment$fieldname"; else $fname = $fieldname; $file = @fopen("files/$fname.txt", "w"); if(!$file) { echo "<h2 style='color:red'>*** ERROR *** unable to open content_$fieldname</h2><h3>But don't panic!</h3>". "Please set the correct read/write permissions to the files folder.<br/> Find the /files/ folder and CHMOD it to 751.<br /><br /> If this still gives you problems, open up the /files/ folder, select all files and CHMOD them to 640.<br /><br /> If this doesn't work, contact me <a href='http://krneky.com/en/contact'>right here</a>."; exit; } fwrite($file, $content); fclose($file); echo $content; // convert udf-8 hexadecimal to decimal function conv($hex) { $dec = hexdec($hex); return "&#$dec;"; } ?>
Hello, I am new to the php community I am trying to build a news feed which shows the first 100 characters of news copy. I have it working to paste in the full text but am not sure how to insert only the first 100 characters. Code: [Select] <?php while($news_row=mysql_fetch_array($news)) { echo " <div class=\"news_box\"> <h3><a href=\"html/news.html\" rel=\"shadowbox; width=800; height=400;\">".$news_row['news_title']."</a> </h3> ".$news_row['news_copy']." </div>"; } ?> Hi People. Thanks to everyone that helped me yesterday with my file that finally connected to the database. However, I have added a new field to my DB and now get further problems. Here is the error message I am getting after I added the "ppr" stuff to both files. Please could someone look at my code and tell me where I'm going wrong. 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>Untitled Document</title> </head> <body> <?php include "config01.php"?> <form name = 'form1' method = 'post' action='config01.php'> <table width="700" border="1" cellspacing="5" cellpadding="5"> <caption> Submit Your Airfield Details </caption> <tr> <td width="100"> </td> <td width="200">Your Name</td> <td width="200"><input type='text' name='username' maxlength='30'></td> <td width="100"> </td> </tr> <tr> <td> </td> <td>Height Above MSL</td> <td><input type='text' name='height_above'maxlength= '30'></td> <td> </td> </tr> <tr> <td> </td> <td>Mb Difference</td> <td><input type='text' name='mb_diff'maxlength='40'></td> <td> </td> </tr> <tr> <td> </td> <td>Alternative Airfield</td> <td><input type='text' name='alternative' maxlength='30'></td> <td> </td> </tr> <tr> <td> </td> <td>PPR</td> <td><input type='radio' name='ppr' value="Y"/> Yes <input type='radio' name='ppr' value="N" /> No</td> <td> </td> </tr> <tr> <td><input type='submit' name='submit' value='post' /></td> <td> </td> <td> </td> <td> </td> </tr> </table> </form> </body> </html> Then the code from config01.php Code: [Select] <?php $host = 'localhost'; $usr = "VinnyG"; $password = 'thepassword'; $db_name = 'sitename'; $username = $_POST["username"]; $height_above = $_POST["height_above"]; $mb_diff = $_POST["mb_diff"]; $alternative = $_POST["alternative"]; $ppr = $_POST["ppr"]; //connect to database mysql_connect ("$host","$usr","$password") or die ('Error During Connect:<br>'.mysql_error()); mysql_select_db ("$db_name") or die ('Error Selecting DB:<br>'.mysql_error()); /* $sql01 = "INSERT INTO users SET username = '$username',height_above = '$height_above', mb_diff = $mb_diff, alternative = $alternative"; $result=mysql_query($sql01); */ //mysql_query("INSERT INTO users VALUES ('$username','$height_above','$mb_diff','$alternative')"); //mysql_query("INSERT INTO users (username, height_above, mb_diff, alternative) VALUES ('$username', '$height_above', '$mb_diff', '$alternative'"); //$insert_query = "INSERT INTO users (username, height_above, mb_diff, alternative) VALUES ('$username', '$height_above', '$mb_diff', '$alternative')"; //$insert_action = mysql_query($insert_query) or die ('Error Dring Insert :<br>'.mysql_error().'<br><br>Error occured running the following code :<br>'.$insert_query); //mysql_query("INSERT INTO users VALUES ('$username','$height_above','$mb_diff','$alternative')"); CHANGES FOLLOW THIS LINE . . $query = "INSERT INTO users (username, height_above, mb_diff, alternative, ppr) VALUES ('$username', '$height_above', '$mb_diff', '$alternative', '$ppr'"; if( !$result = mysql_query($query) ) { echo "<br>Query: $query<br>Produced error: " . mysql_error() .'<br>'; } else { echo "Query ran and inserted " . mysql_affected_rows() . "row(s)."; } ?> I only added the one thing and I've gone and broken it already. It was working last night too.. :-( is this part of code correct ? Code: [Select] $query = mysql_query("select * from username WHERE username='$username'"); if(mysql_query($query) > 0) { die("Username already in use."); } else { Hello.
How would I print the following:
http://site.com/break?return=site2.com
So that I can set a button containing site2.com.
Example: <a href="site2.com"></a>
And so forth:
http://site.com/break?return=site3.com
Would print: <a href="site3.com"></a>
I'm using PHP to get a list of names from a mysql database, which then produces a drop down list for the user to select an option and then progress to the next page where the selection is displayed with other stuff. My problem is that my drop down list works fine, but when the user goes to the next page only the first part of the string is displayed, ie if the string is Fred Bloggs, only Fred is shown. I can get it to work by adding ' ' around the string name in the option section, but this shows 'Fred Bloggs' in the drop down list which isn't very pretty. I haven't got the actual code to hand at the moment, but any ideas on where I'm going wrong? Cheers The program is working for part of the data . It can display in the CSS and HTML code, however, the other data can not display . I do not why . They are same structure . Please tell me the reason. Thank you very much. Hello guys, I want to remove part of a string and convert it to a var instead of echoing it out.. How would I do it.. $string = "temp_photo/testing.jpg"; I want to remove temp_photo so the out put should be $filename2 = testing.jpg Please advise.. Thanks, Dan I need some help with the category part on a old project . The problem what I have at this moment that, when I click on the category all categories are showing up on the category.php page itself. What whI wanne have when I click Uncategorised that in the category.php page only post are showing that have been made in Uncategorised category function get_posts() { $sql = "SELECT `blog`.`post_id` AS `id`, `blog`.`post_title` AS `title`, LEFT(`blog`.`post_body`, 512) AS `preview`, `blog`.`post_user` AS `user`, `blog_categories`.`id` AS `category_id`, `blog_categories`.`name` AS `category_name`, DATE_FORMAT(`blog`.`post_date`, '%d-%m-%Y %H:%i') AS `date`, `blog_comments`.`total_comments`, DATE_FORMAT(`blog_comments`.`last_comment`, '%d-%m-%Y %H:%i') AS `last_comment` FROM `blog` INNER JOIN `blog_categories` ON `blog_categories`.`id` = `blog`.`cat_id` LEFT JOIN ( SELECT `post_id`, COUNT(`comment_id`) AS `total_comments`, MAX(`comment_date`) AS `last_comment` FROM `blog_comments` GROUP BY `post_id` ) AS `blog_comments` ON `blog`.`post_id` = `blog_comments`.`post_id` ORDER BY `blog`.`post_date` DESC"; $posts = mysql_query($sql); $rows = array(); while (($row = mysql_fetch_assoc($posts)) !== false) { $rows[]= array( 'id' => $row['id'], 'title' => $row['title'], 'preview' => $row['preview'], 'user' => $row['user'], 'date' => $row['date'], 'category_id' => $row['category_id'], 'category_name' => $row['category_name'], 'total_comments' => ($row['total_comments'] === null) ? 0 : $row['total_comments'], 'last_comments' => ($row['last_comment'] === null) ? 'never' : $row['last_comment'] ); } return $rows; } function get_post($pid) { $pid = (int)$pid; $sql = "SELECT `post_title` AS `title`, `post_body` AS `body`, `post_user` AS `user`, `blog_categories`.`id` AS `category_id`, `blog_categories`.`name` AS `category_name`, DATE_FORMAT(`post_date`, '%d-%m-%Y %H:%i') AS `date` FROM `blog` INNER JOIN `blog_categories` ON `blog_categories`.`id` = `blog`.`cat_id` WHERE `post_id` = '$pid'"; $post = mysql_query($sql); $post = mysql_fetch_assoc($post); $post['blog_comments'] = get_comments($pid); return $post; }
The old way was like this if ( isset($cat_id)) { $cat_id = (int) $cat_id; $query .= " WHERE `blog_categories`.`id` = $cat_id"; } Database blog_categories id | name | ----------- 4 Uncategorised 1 Youtube blog post_id | cat_id | post_title | post_body | post_user | post_date ----------------------------------------------------------------------------------- 4 | 1 | Youtube | Youtube link | Admin | 2020-09-13 16:10:53 Web server Apache/2.2.21 (Win64) PHP/5.3.10 MySQL-client versie: mysqlnd 5.0.8-dev - 20102224 - $Revision: 321634 $ PHP uitbreiding: mysqli Documentatie phpMyAdmin Versie informatie: 3.4.10.1, meest recente versie: 5.0.2 I hope someone can help me with this problem |