PHP - Limiting The Amount Of Returned Characters From Variable
$text = "this is a lot of effing text" what i want to do is limit it to the 10 characters so it gives back $stext = substr($text, 10); <-- this is how i thought it would work echo " $stext"; Similar Tutorialsif i want to display upcoming events and only want to show the top 3? Im not sure f this is the right spot for this or not but... I have a website that has a content box that has a fixed height and width. How would i make it so after the box is full it puts a link to read more at the bottom of it? Also images may be used as well. Its also for a wordpress blog I have a variable called $content and I want it to stop execution at 25 characters... How can this be accomplished? Thanks in advance. If I knew what to call it I could have searched it. So I will just explain. I have a form that is pulling from a database to build the following. echo <input name='$row[id]' type='text' id='$row[id]' size=2>; This will build a page that has this <input name='22' type='text' id='22' size=2> <input name='54' type='text' id='54' size=2> This part works great. The problem is when I try to add it to the database. How can I insert variables that I don't know the names of? They will be something like $_POST[22] $_POST[54] and so on. I hope someone can understand my jibberish. I am sure there is a WAY better way to do this. Hi, I have a function where I want to sort an an array of this type: 0 = {'fruit' => 'banana', 'color' => 'yellow', 'age' => '1', 'weight' => '2', 'brand' => 'dole', 'store' => shaws'} 1 = {'fruit' => 'apple', 'color' => 'red', 'age' => '3', 'weight' => '5', 'brand' => 'fuji', 'store' => 'foodmaster'} etc etc. I then want to array_multisort this array with different parameters. One way is to sort by fruit, then color, then brand. The second way by age, then store. Code: [Select] //This is how I sort it for my first way //Let's call the array $rows function sort($rows) { foreach($rows as $key => $value) { $fruit[$key] = $value['fruit']; $color[$key] = $value['color']; $brand[$key] = $value['brand']; } array_multisort($fruit, SORT_ASC, $color, SORT_ASC, $brand, SORT_ASC $rows); } I want to try to reuse this function by passing in the keys that I want to sort with. Like doing sort($rows, array('fruit', 'color', 'brand')), then sort($rows, array('age', 'store')). Is this possible? I'm creating a website where writers can share stories. I have a section where the users enter a summary of there story. Anyways in the area where the story in being listed I want the the title of the story(with a link to the story) underneath the link I would like a summary. BUUUUT, the summary can get pretty long(up to 900 chars). So I am wondering if I can shorten the variable where the summary is stored, I want it to be shorten to a fixed amount of characters(maybe 200?) then followed by a ... . So how can I do this? Heres what i got... function printLB1 (){ $result = mysql_query("SELECT * FROM leaderboards ORDER BY CollegeFootballPoints DESC"); while ($row = mysql_fetch_object($result)) { $leaderboard[] = $row->Username; $leaderboardPoints[] = $row->CollegeFootballPoints; } $num = mysql_num_rows($reault); //I know from here to ................... needs to be in a var or echo or something. <tr> <td>1.</td> //This will auto increment too like i++ but i cna do that myself! <td>echo $leaderboard;</td> <td>echo $leaderboardPoints;</td> </tr> //Here......................................................... } I need to pull a table row per user. But i want to somehow do it once in a function and then ill echo the function into a table after the php stuff is done. Like i want to pull every a table row per user in the function. then display the function below that way i don't have to write a whole extra query and table row per person. If you understand please help if not please let me know where i can explain more. Thanks Hi, I am trying to make some adjustments to uploadify.php which comes with the latest version of uploadify (3.0 beta), so that it works with a session variable that stores the login username and adds it to the path for uploads. Here is uploadify.php as it currently looks: Code: [Select] <?php session_name("MyLogin"); session_start(); $targetFolder = '/songs/' . $_SESSION['name']; // Relative to the root if (!empty($_FILES)) { $tempFile = $_FILES['Filedata']['tmp_name']; $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder; $targetFile = rtrim($targetPath,'/') .'/'. $_FILES['Filedata']['name']; // Validate the file type $fileTypes = array('m4a','mp3','flac','ogg'); // File extensions $fileParts = pathinfo($_FILES['Filedata']['name']); if (in_array($fileParts['extension'],$fileTypes)) { move_uploaded_file($tempFile,$targetFile); echo '1'; } else { echo 'Invalid file type.'; } } echo $targetFolder; ?> I added Code: [Select] echo $targetFolder; at the bottom so that I could make sure that the string returned was correct, and it is, i.e. '/songs/nick'. For some reason though, uploads are not going to the correct folder, i.e. the username folder, but instead are going to the parent folder 'songs'. The folder for username exists, with correct permissions, and when I manually enter Code: [Select] $targetFolder = '/songs/nick';all works fine. Which strikes me as rather strange. I have limited experience of using php, but wonder how if the correct string is returned by the session variable, the upload works differently than with the manually entered string. Any help would be much appreciated. It's the last issue with a website that was due to go live 2 days ago! Thanks, Nick I have the following code in my index.php: <?php $files = array(); $files = glob('download/*.iso'); $file = $files[count($files) - 1]; $strlen ( string $file ) : int /* length of $file */ $filelen = $strlen -9 /* length of unwanted characters - "download/" */ $filename = /* how do I get the filename starting at the 10th position */ ?> <div class="container"> <div class="divL"> <h3>Get the latest version of FoxClone iso</h3> <a href="<?php echo "/{$file}";?>"><img src="images/button_get-the-app.png" alt="" width="200" height="60"></a> I'd like to replace "Get the latest version of FoxClone iso" in the next to last line with "Get Foxclone "<?php echo "/{$filename}";?>". How do I extract just the file name from $file? I know that I have to:
1. get the length of $file Step 3 is where I just don't know how to accomplish the task. I'd appreciate some help on this.
Thanks in advance, Hi, I am wondering if it is possible to monitor how long someone is on a website for and to limit them for example: 30 minutes. An example of this would be one of those Tv Online websites which allow you to only watch 30 minutes of video time. If you try refreshing the page it will still not allow you. My question is, Is it an ip check which does this? Sorry If it such a broad topic. Nick OMG! Crafting well thought out business logic is such a PITA sometimes! I am working on an ecommerce site that sells subscriptions, books, gear, etc. Similar to a lot of online newspapers, I have a "Subscribe" button in my website mast where people can buy a subscription. After clicking on that button, the user is first presented with different subscription offers (e.g. sliver, gold, platinum), and then I have a one-page checkout form where they create an account - kind of important for an online subscription! - and they pay with a credit card. Easy! Trying to be a nice guy, I decided to add the ability to choose a subscription from my online store as well. (Presumably you want to offer as many ways for people to buy things from you as possible, right?) Well here is the issue I just discovered... What happens if a person is browsing through my product catalog, and they add multiple subscriptions to their shopping cart? Of course I would welcome the extra $$$, but I just realized that would break how people create their accounts and pay, because above I assume that ONE person, buys ONE subscription and pays for it in ONE transaction. So my questions is, "Would it be a mortal sin to limit people to buying only ONE subscription at a time if they do so while in my online store?" I would hope that people would get that, but as we know, users do some crazy stuff!! In fairness, if you went to buy a subscription at the Wall Street Journal or any other large newspaper/magazine, you would be forced to do one subscription per transaction. I am just wondering what happens if someone goes on a shopping spree in my online store, buys several books, some t-shirts, and decides he/she also wants to buy 5 subscriptions on the spot. Thoughts?
function wsProcessClientMessage($clientID, $opcode, &$data, $dataLength) { global $wsClients; // check opcodes if ($opcode == WS_OPCODE_PING) { // received ping message return wsSendClientMessage($clientID, WS_OPCODE_PONG, $data); } elseif ($opcode == WS_OPCODE_PONG) { // received pong message (it's valid if the server did not send a ping request for this pong message) if ($wsClients[$clientID][4] !== false) { $wsClients[$clientID][4] = false; } } elseif ($opcode == WS_OPCODE_CLOSE) { // received close message if (substr($data, 1, 1) !== false) { $array = unpack('na', substr($data, 0, 2)); $status = $array['a']; } else { $status = false; } if ($wsClients[$clientID][2] == WS_READY_STATE_CLOSING) { // the server already sent a close frame to the client, this is the client's close frame reply // (no need to send another close frame to the client) $wsClients[$clientID][2] = WS_READY_STATE_CLOSED; } else { // the server has not already sent a close frame to the client, send one now wsSendClientClose($clientID, WS_STATUS_NORMAL_CLOSE); } wsRemoveClient($clientID); } elseif ($opcode == WS_OPCODE_TEXT || $opcode == WS_OPCODE_BINARY) { // received text or binary message if (function_exists('wsOnMessage')) wsOnMessage($clientID, $data, $dataLength, $opcode == WS_OPCODE_BINARY); } else { // unknown opcode return false; } return true; } Ok so here's my code first the ajax/javascript, secondly the php. Issue i'm having is that first it wasn't entering any data into my database then eventual through much troubleshooting I figured out that if I add more then 10 variables in my INSERT INTO query it wont work. Ten or less and it works perfect. I CAN NOT figure this out?! Note, at the bottom of the php script I have the script that I would like to work, commented out, and the script that is currently working, active. You can see it only has 10 working entries. Code: [Select] function entervehicle(){ var stk = document.getElementById("stk").value var my = document.getElementById("my").value var mak = document.getElementById("mak").value var mod = document.getElementById("mod").value var mil = document.getElementById("mil").value var pri = document.getElementById("pri").value var bst = document.getElementById("bst").value var len = document.getElementById("len").value var eng = document.getElementById("eng").value var dor = document.getElementById("dor").value var tra = document.getElementById("tra").value var gvw = document.getElementById("gvw").value var inc = document.getElementById("inc").value var exc = document.getElementById("exc").value var cfo = document.getElementById("cfo").value var titl = document.getElementById("titl").value var desc = document.getElementById("desc").value var vin = document.getElementById("vin").value var pic1 = document.getElementById("pic1").value var pic2 = document.getElementById("pic2").value var pic3 = document.getElementById("pic3").value var pic4 = document.getElementById("pic4").value var pic5 = document.getElementById("pic5").value var pic6 = document.getElementById("pic6").value var pic7 = document.getElementById("pic7").value var pic8 = document.getElementById("pic8").value var pic9 = document.getElementById("pic9").value var linksubmit ="../php/entervehicle.php?stk=" + stk +"&my=" + my +"&mak=" + mak +"&mod=" + mod +"&mil=" + mil +"&pri=" + pri +"&bst=" + bst +"&len=" + len +"&eng=" + eng +"&dor=" + dor +"&tra=" + tra +"&gvw=" + gvw +"&inc=" + inc +"&exc=" + exc +"&cfo=" + cfo +"&titl=" + titl +"&desc=" + desc +"&vin=" + vin +"&pic1=" + pic1 +"&pic2=" + pic2 +"&pic3=" + pic3 +"&pic4=" + pic4 +"&pic5=" + pic5 +"&pic6=" + pic6 +"&pic7=" + pic7 +"&pic8=" + pic8 +"&pic9=" + pic9 alert(linksubmit); var entrequest = getHTTPObject(); if(entrequest) { entrequest.open("GET",linksubmit,true); entrequest.onreadystatechange = function(){ entveh(entrequest); }; entrequest.send(null); } } function entveh(entrequest){ if(entrequest.readyState == 4){ if(entrequest.status == 200){ var myPHP = entrequest.responseText; document.getElementById("adminmain").innerHTML = myPHP; } } } <?php $stk = $_GET['stk']; $my = $_GET['my']; $mak = $_GET['mak']; $mod = $_GET['mod']; $mil = $_GET['mil']; $pri = $_GET['pri']; $bst = $_GET['bst']; $len = $_GET['len']; $eng = $_GET['eng']; $dor = $_GET['dor']; $tra = $_GET['tra']; $gvw = $_GET['gvw']; $inc = $_GET['inc']; $exc = $_GET['exc']; $cfo = $_GET['cfo']; $titl = $_GET['titl']; $desc = $_GET['desc']; $vin = $_GET['vin']; $pic1 = $_GET['pic1']; $pic2 = $_GET['pic2']; $pic3 = $_GET['pic3']; $pic4 = $_GET['pic4']; $pic5 = $_GET['pic5']; $pic6 = $_GET['pic6']; $pic7 = $_GET['pic7']; $pic8 = $_GET['pic8']; $pic9 = $_GET['pic9']; require("database.php"); $tresult = mysql_query("INSERT INTO automobile_stat (stock_number,model_year, make, model, miles, price, body_style, length, description, trans) VALUES ('$stk', '$my', '$mak', '$mod', '$mil', '$pri', '$bst', '$len', '$desc', '$tra')"); /*$tresult = mysql_query("INSERT INTO automobile_stat (stock_number, model_year, make, model, miles, price, body_style, length, description, engine, doors, trans, gvw, int_color, ext_color, car_fax_owner, title_desc, main_picture_src, vin) VALUES ('$stk', '$my', '$mak', '$mod', '$mil', '$pri', '$mpri', '$bst', '$len', '$desc', '$eng', '$dor', 'tra', '$gvw', '$inc', '$exc', '$cfo', '$titl', 'img', '$vin')");*/ /*$tresult=mysql_query("SELECT * FROM automobile_stat" ); $countrows = mysql_num_rows($tresult); while($row = mysql_fetch_array($tresult)){ echo $row[stock_number]; }*/ mysql_close($linkID); ?> Following on from the excellent help in the thread at http://www.phpfreaks.com/forums/php-coding-help/using-a-generated-row-number-in-another-query I have another question regarding the League Standings that are generated there. I want to have another version that limits the records to the last 5 games for each team. Just using 'LIMIT 5' would return the first five teams and all their records but I want it to retrieve all of the teams and each teams last five records (games). The easy way would be to restrict a the dates but as games get postponed and moved using WHERE with a specific date would not guarantee the same number of games for each team. There is the possibility that this could be done via the query but I have not come across anything in some quite extensive searching (although, once again, I do not know if I am using the right search terms!) so I assume PHP would, once again be the saviour! Thanks in advance for any suggestions. Steve Hey there - I have been trying to limit the number of comments a user can make per day on my social network. Thanks to great help on here I am getting really close, however, there are some bugs that have me banging my head against the wall. I am able to limit, but now, its 1. not limiting PER day and limiting all around ( meaning: I can't make any comments at all today ) 2. it is limiting for EVERY user as opposed to limiting a specific user Here is the code I have: Code: [Select] if(isset($_POST['commentProfileSubmit'])) { if($_POST['ProfileComment'] == "" || $_POST['ProfileComment'] == "Tell the community what's on your mind...") { $valid = false; $error_msgs_comments[] = "Whoops! You forgot to write your airwave."; }else{ if($_POST['ProfileComment'] == "" || $_POST['ProfileComment'] == "Leave ".$prof->first_name." a comment here...") { $valid = false; $error_msgs_comments[] = "Whoops! You forgot to write your comment."; }else{ /* if the person signed in is NOT the profile */ $query = "SELECT * FROM `cysticUsers` WHERE `id` = '" . $prof->id . "'"; $request = mysql_query($query,$connection) or die(mysql_error()); $result = mysql_fetch_array($request); $max_post_per_day = 5; $Email = $result['Email']; $check_profi = $result['check_profi']; $check_reply = $result['check_reply']; if($prof->id != $auth->id && $check_profi == 'checked' && $max_post_per_day < 5) { $to = $Email; $subject = "$auth->first_name $auth->last_name commented on your profile on CysticLife"; $message = "$auth->first_name $auth->last_name commented on your profile on CysticLife: <br /><br />\"$body\"<br /><br /> <a href='http://www.cysticlife.org/Profile_build.php?id=" . $prof->id . "'>Click here to view</a><br /><br />Do LIFE,<br /> The CysticLife Team"; $from = "CysticLife <noreply@cysticlife.org>"; $headers = 'MIME-Version: 1.0' . "\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n"; $headers .= "From: $from"; mail($to, $subject, $message, $headers); } $query = "SELECT COUNT(*) FROM `CysticAirwaves` WHERE `FromUserID` = $auth->id AND `date` = CURDATE()"; $result = mysql_query($query, $connection); $post_count = mysql_result($result, 0); if($post_count >= $max_posts_per_day) { echo "You have reached the maximum number of posts for the day. Try again tomorrow"; } else { $comment = mysql_real_escape_string($_POST['ProfileComment']); $query = "INSERT INTO `CysticAirwaves` (`FromUserID`, `ToUserID`, `comment`, `status`, `statusCommentAirwave`, `date`, `time`) VALUES ('{$auth->id}', '{$prof->id}', '{$comment}', 'active', 'active', CURDATE(), CURTIME())"; mysql_query($query, $connection) or die(mysql_error()); } if($auth->id == $prof->id) { $just_inserted = mysql_insert_id(); $query = "UPDATE `CysticAirwaves` SET `status` = 'dead' WHERE `FromUserID` = '" . $auth->id . "' AND `ToUserID` = '" . $prof->id . "' AND `id` != '" . $just_inserted . "'"; $request = mysql_query($query,$connection); } } } } thanks so much in advanced I have a database of 100 lines which have echoed onto the page so each line within the database is printed. I would like to limit it to ten. However, I have tried a whole range of different loops but none of them seek to work. Some of them print the database ten times so I have 1000 lines on the page. Can anyone advise the best method of limiting how many lines within a database are printed on the page? Hi. Trying to use the "Excerpt or Content Word Limit in WordPress: Redux" code to limit my Amazon product description content but to no avail. Getting the "Fatal error: Call to undefined function content() in index.php on line 36". The following is the code in the index.php file that makes the call: " <div class="post-content"> <?php content(30); ?> <!-- <?php the_post_excerpt($excerpt_length=30); ?> --> </div><!-- POST CONTENT --> " The functions.php file is quite large but I attached it so anyone who would like to take a look at it. Can anyone tell why it's not finding the function 'content'? Thanks! hello all - I have a social network for the cystic fibrosis community and we've had a spammer the passed couple of days. I have captchas set up and they work well, but in addition to those I want to limit the amount of blogs a user is allowed to post a day. I have been able to count and echo out the amount a user has made, but for some reason, when I use an if statement to stop the blog from posting it still post's. I have been working on this going on hour 8 now and need some major help Thanks in advnace. Here is my code Code: [Select] if(isset($_POST['subComposeBlog'])) { $query = "SELECT COUNT(`id`) FROM `cysticBlogs` WHERE `Author` = '".$auth."' && `date` = NOW() && `status` = 'active'"; $request = mysql_query($query,$connection) or die(mysql_error()); $result = mysql_fetch_array($request); $valid = true; if($_POST['Category'] == "null") { $valid = false; $error_msgs[] = "Whoops! Please select a category for this blog."; } if(empty($_POST['blogTitle'])) { $valid = false; $error_msgs[] = "Whoops! Cannot submit a blog without a title,how are you going to attract people's attention to read your masterpiece?"; } if(empty($_POST['blogBody'])) { $valid = false; $error_msgs[] = "Whoops! Cannot submit a blog without a body,that would not be a blog now would it?"; } if($result['COUNT(`id`)'] > 3) { $valid = false; echo "Whoops! You can only write three blogs per day due to spam"; } if($valid) { $query = "INSERT INTO `cysticBlogs` ( `blogTitle`, `blogBody`, `date`, `time`, `Author`, `Category` ) VALUES ( '" . mysql_real_escape_string($_POST['blogTitle']) ."', '" . mysql_real_escape_string($_POST['blogBody']) ."', '" . date("Y-m-d") ."', '" . date("G:i:s") ."', '" . $auth->id ."', '" . mysql_real_escape_string($_POST['Category']) ."')"; mysql_query($query, $connection) or die (mysql_error()); header("Location: BlogsSecurity.php"); } } |