PHP - How To Repeat An Output Until The Last Row Then Assign A Different Style To It?
Hey guys, bascially I'm developing a simple news script, and part of it has a "quick news" section, which will display the latest 3 (or any number I want) news items, each seperated by a line (<hr> in the example below).
What I want to acheive is the top 2 news items have a set LI class assigned to them, and then the 3rd LI in the list to have a different style attached to it (one without a bottom border basically), so it'll bascially repeate out the normal <li> until it hits the last item to be output, at which point it uses a LI with a class attached to it. What I don't know how to do, is tell the PHP that once it's out putted 2 LI's, then the 3rd LI is to have a different class on it. eg Code: [Select] <li>News Item 1</li> <li>News Item 2</li> <li class="last">News Item 3</li> I'm pretty new to PHP so not sure how to achieve this and would appreicate any help you can offer. Here's my code so far... Code: [Select] <?php $latestnews_sql = "SELECT * FROM news ORDER BY date DESC LIMIT 3"; $latestnews_query = mysql_query($latestnews_sql) or die (mysql_error()); $list_latestnews = mysql_fetch_assoc ($latestnews_query); ?> <ul> <?php do { ?> <li><p><strong><?php echo $list_latestnews['title']; ?></strong></p> <?php echo substr($list_latestnews['body'],0,80)."...";?> <a href="news-detail.php?ID=<?php echo $list_latestnews['ID']; ?>">view more</a></p> <p><span style="font-size:13px; font-style:italic;"><?php echo $list_latestnews['author']; ?> - <?php echo date("l, jS F Y", strtotime($list_latestnews['date'])); ?></span></p> <hr /> </li> <?php } while ($list_latestnews = mysql_fetch_assoc ($latestnews_query)) ?> </ul> Similar TutorialsI have a script that opens up a CSV file and outputs to a table for testing. It ultimately inserts into a MySQL but the code below literally duplicates every third row consistently. I'm 95% sure its in the "WHILE" area. Is there enough code here for someone to point out an error. If not I can put the code up live and if you want a fee for fixing just PM me please. Code: [Select] $fp = fopen("residential-data.csv", "r"); $listings=-1; function getZip($full) { $pieces = explode(' ', $full); $zip = explode('(', $pieces[count($pieces)-1]); $zip = explode(')', $zip[1]); return $zip[0]; } $pre=""; while (($data = fgets($fp,4096)) !== FALSE) { $data=str_replace("\r\n","",$data); if(trim($data)=="" or substr($data,strlen($data)-1,1)!='"' or substr($data,strlen($data)-2,2)==',"') { $pre.=$data; } Well I have a script that executes a scan on a system set to run infinitely, and I need it to echo out a message each time it loops through, but I don't want it to echo out the message with the next loop message below it, and the next one below that etc... I've tried using the flush(); function and been messing around with that with no luck. For security reasons I don't want to release any of the processing code, but here is the basic construction of the script: <?PHP ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** $RepeatIt = -1; for($g=1; $g!=$RepeatIt+1; $g++) { ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** $ScanMessage = ":.:.: SCANNING THE HITLIST FOR MOBSTER: ".$MobName." (SCAN #$g) :.:.:"."<br/><br/>"; echo $ScanMessage; ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** } ?> At the moment it's returning: :.:.: SCANNING THE HITLIST FOR MOBSTER: DEUS EX DESTROYER (SCAN #1) :.:.: :.:.: SCANNING THE HITLIST FOR MOBSTER: DEUS EX DESTROYER (SCAN #2) :.:.: :.:.: SCANNING THE HITLIST FOR MOBSTER: DEUS EX DESTROYER (SCAN #3) :.:.: :.:.: SCANNING THE HITLIST FOR MOBSTER: DEUS EX DESTROYER (SCAN #4) :.:.: So what I want it to do is just delete the scanning message and replace it with the next scan message so while running this script you would see just the number increment on the same line. Any suggestions? Thanks. alright, soI want to query a table and grab the last record in a desc order and assign it plus one. My code so far for testing is: <?php ini_set('display_errors',1); error_reporting(E_ALL|E_STRICT); include "connect.php"; $result = mysql_query("SELECT * FROM tickets ORDER BY id DESC LIMIT 1"); echo "<table cellpadding='4' cellspacing='4'><tr> <th>id</th></br> </tr>";while($row = mysql_fetch_array($result)) { echo "<tr><td>" . $row['id'] . "</td></tr>"; } echo "</table>"; ?> So I can display the returned value, but how can I assign the returned value to a php variable? does anyone knows how to fill this value to a variable (code below works on the screen): Code: [Select] (str)$txt->value; echo (str)$txt->value; I need to get the value from the code above into $myvalue (code below does not work): Code: [Select] (str)$txt->value; $myvalue = (str)$txt->value; echo $myvalue; Hi All,
I hope someone can help!
I've got some code that passes an array of values via an ajax call. This works fine and I can see my values in the specified div:
$("#container").html(data);What I'd like to do, is get that info and pass it to a cookie. However, I'm struggling a tad. I currently have this: var set_me = html(data); $.cookie('choice',set_me, {expires: 7, path: '/'}); alert( $.cookie("choice") );But sadly it's not working. I think it's something simple but this is new to me some need some help please Thanks Hi everybody, im reading up on arrays and im still stuck... Is it possible to assign an array to a variable? I need something like... Code: [Select] &needvariable = $row->User Hopefully this makes sense... Hi, I'm trying to implement multithreading using Threadi https://github.com/danielpoe/Threadi Here is a sample code to what I'm trying to do: Code: [Select] require_once(dirname(__FILE__).'/../Loader.php'); class Worker { public function fetchAWebsite($url) { $content = file_get_contents($url); return "fetched website $url - lenght ".strlen($content).PHP_EOL; } } $worker = new Worker(); $startTime = microtime(TRUE); $thread1 = Threadi_ThreadFactory::getReturnableThread(array($worker,'fetchAWebsite')); $thread1->start('http://www.google.de'); $thread2 = Threadi_ThreadFactory::getReturnableThread(array($worker,'fetchAWebsite')); $thread2->start('http://www.aoemedia.de'); $joinPoint = new Threadi_JoinPoint($thread1, $thread2); $joinPoint->waitTillReady(); echo $thread1->getResult(); echo $thread2->getResult(); echo "Time needed in seconds: ". ( microtime(TRUE)-$startTime).PHP_EOL; The problem that I'm having is that I need to call each thread separately by name. Creating 10 or more threads makes a lot redundant code. How can I assign Threadi_ThreadFactory::getReturnableThread to an indexed variable(array) ? I tried something in this lines but with no success: Code: [Select] $hArr = array();//handle array for ($i=0;$i<THREADS;$i++) { $thread[$i] = Threadi_ThreadFactory::getReturnableThread(array($worker,'fetchAWebsite')); $thread[$i]->start($site[$i]); array_push($hArr,$thread[$i]-); } $joinPoint = new Threadi_JoinPoint($hArr); $joinPoint->waitTillReady(); What I'm doing wrong? Thanks I have a page that lists out some basic information about each quiz this particular student has taken (call it "quiz summary"). I am creating another page that will have all details of a particular quiz by itself (call it "quiz detail"). If I have a link for each quiz on the "quiz summary" page, how do I tell the "quiz detail' page which link they clicked on? Or which quiz they have selected. I was thinking it would work if I assigned a session variable to each link and then the "quiz detail" page could verify which returns true still and show details for that quiz. Would this logic work? Npot sure how to do it anyone though. thanks for any help! How do I assign the entire $_POST array to a varible? This code isn't working... <?php echo 'FormHandler'; class FormHandler{ private $formValues = array(); $this->formValues = $_POST; } ?> TomTees hey everyone will you please assist. I'm getting data from a mysql database. And I want to assign the data to a single value, if possible For example, in my database I have 2 columns, name and group Lets say John, Peter and Cathy belongs to group 555, where Sally and Marcus belongs to group 777. So my sql query outputs all users in group 555 Is there a way to assign the search result to a vlue, for example $result = ??? and If I said echo $result, it should show the users who belongs to group 555 as an example. I tried to use row[Name][1]; but it doesnt work. The thing is, I can display the data the way I am familiar with, but I'm sending an automated email to myself, containing the users. So if I would used a for each statement, the mail will trigger and send a copy for every count, in this case, 3 times. And thats my huge problem. The mail must trigger only once, containing the users in my search query, in this case, group 555. Any help will be appreciated Thank you I am trying to convert a php4 app to php5 and am getting this error (Fatal error: Cannot re-assign $this in ) when I call this class. $this = $this->createUnique($new); is the line that generates the error. I try to rename $this to $_this but it does not help. I think I lose the reference to the object later on in the code. Then I will get an error like (Fatal error: Call to a member function createUnique() on a non-object in) Is there another way to reference the current instance of an object in php5? Any help appreciated... Code Example: Code: [Select] class imageobject{ var $handle; var $height=0; var $width=0; var $directory; var $filename; //constructor function imageobject($directory,$filename,$width=0,$height=0,$color="FFFFFF") { $this->directory = $directory; $this->filename = $filename; if ($filename=="" && $width>0 && $height>0){ $new = $this->createImage($width,$height); $this = $this->createUnique($new); }elseif (file_exists($directory.$filename)){ $size = GetImageSize($directory.$filename); if ($size) $this->handle = $this->getHandle($directory.$filename,$size[2]); $this->width = $size[0]; $this->height = $size[1]; } function createUnique($imgnew) { $this->type = substr($this->output,0,3); $unique_str = $this->uniqueName(); switch ($this->type){ case "png": imagepng($imgnew,RES_DIR.$unique_str); break; default: imagejpeg($imgnew,RES_DIR.$unique_str,$this->quality); break; } $this->handle && imagedestroy($this->handle); $newobject = new imageobject(RES_DIR,$unique_str,$this->type); return $newobject; } )//end class Hello All, I have two php files original.php and return.php retrun.php file returns a variable after I pass $channel_name to it Code: [Select] return $return_variable; I want to assign this return variable to a variable in original.php Something like this Code: [Select] $results = include ('return.php?channel='.$channel_name.'');I am not sure how to do the above code. When I call the return.php from a browser it works properly. Thanks for any help Hey guys, im trying to make a dropdown box get a selected value based on the value from multiple rows. Here is what im trying to do: <?php // select the value from the database // $value = ... ?> <form action="..."> <option value="1"<?php if ($value == 1) { echo ' selected="selected"'; } ?>>First</option> <option value="2"<?php if ($value == 2) { echo ' selected="selected"'; } ?>>Seccond</option> <option value="3"<?php if ($value == 3) { echo ' selected="selected"'; } ?>>Third</option> <input type="submit"> </form> The thing is I have no idea what to put at the top and how to assign the $value part. Here is my current code. <?php mysql_connect("localhost", "", "")or die("cannot connect"); mysql_select_db("test")or die("cannot select DB"); $tbl_name="test_mysql"; $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $count=mysql_num_rows($result); // MOVED this code to the top before you output anything if (isset($_POST['Submit'])) { for($i=0;$i<$count;$i++){ $month = $_POST['month']; $date = $_POST['date']; $message = $_POST['message']; $title = $_POST['title']; $id = $_POST['id']; $icon = $_POST['icon']; // ILYA $monthday= $month[$i]."<br>".$date[$i]; $sql1="UPDATE $tbl_name SET monthday='$monthday', month='$month[$i]', date='$date[$i]', message='" . mysql_real_escape_string($message[$i]) . "', title='" . mysql_real_escape_string($title[$i]) . "', icon='$icon[$i]' WHERE id=".$id[$i]; // ILYA if(!($result1 = mysql_query($sql1))){ "<BR>Error UPDATING $tbl_name "; exit(); } } } $result=mysql_query($sql); $count=mysql_num_rows($result); ?> Here is the selection dropdown box im trying to make work: <select name="icon[]" style="display:blolck; width:200px" class="mydds"> <option value="1" title="icon/icon_phone.gif">Phone</option> <option value="2" title="icon/icon_sales.gif">Graph</option> <option value="3" title="icon/icon_faq.gif">Faq</option> </select> The thing is, it gets multiple rows at once and I want to make it select each one differently based on the rows value. I dont want it to like interfere. Do you kinda know what I mean? Also, heres hows the database looks like. Picture of webpage. Here is the full code if anyone would like to see: <?php mysql_connect("localhost", "", "")or die("cannot connect"); mysql_select_db("test")or die("cannot select DB"); $tbl_name="test_mysql"; $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $count=mysql_num_rows($result); // MOVED this code to the top before you output anything if (isset($_POST['Submit'])) { for($i=0;$i<$count;$i++){ $month = $_POST['month']; $date = $_POST['date']; $message = $_POST['message']; $title = $_POST['title']; $id = $_POST['id']; $icon = $_POST['icon']; // ILYA $monthday= $month[$i]."<br>".$date[$i]; $sql1="UPDATE $tbl_name SET monthday='$monthday', month='$month[$i]', date='$date[$i]', message='" . mysql_real_escape_string($message[$i]) . "', title='" . mysql_real_escape_string($title[$i]) . "', icon='$icon[$i]' WHERE id=".$id[$i]; // ILYA if(!($result1 = mysql_query($sql1))){ "<BR>Error UPDATING $tbl_name "; exit(); } } } $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <!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>Image Dropdown</title> <link rel="stylesheet" type="text/css" href="dd.css" /> <script type="text/javascript" src="jquery-1.3.2.min.js"></script> <script type="text/javascript" src="jquery.dd.js"></script> </head> <body> <form name="form1" method="post" action="updat5.php"><!--ILYA--> <tr> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center"><strong>ID</strong></td> <td align="center"><strong>Month - Date</strong></td> <td align="center"><strong>Message</strong></td> <td align="center"><strong>Title</strong></td> <td align="center"><strong>Icon</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center"><input style="border: 1px solid #C3C3C3;height: 20px; width: 20px;" id="id" name="id[]" value="<?php echo $rows['id']; ?>"></td> <td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="month[]" MAXLENGTH="3" size="3" type="text" id="month" value="<?php echo $rows['month']; ?>"> <input style="border: 1px solid #C3C3C3;height: 20px;" name="date[]" MAXLENGTH="2" size="2" type="text" id="date" value="<?php echo $rows['date']; ?>"> </td> <td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="message[]" size="70" type="text" id="message" value="<?php echo $rows['message']; ?>"></td> <td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="title[]" size="70" type="text" id="title" value="<?php echo $rows['title']; ?>"></td> <td align="center"> <select name="icon[]" style="display:blolck; width:200px" class="mydds"> <option value="1" title="icon/icon_phone.gif">Phone</option> <option value="2" title="icon/icon_sales.gif">Graph</option> <option value="3" title="icon/icon_faq.gif">Faq</option> </select> </td> </tr> <?php } ?> <tr> <td colspan="4" align="center"><br><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </td> </tr> </form> <script language="javascript" type="text/javascript"> function showvalue(arg) { alert(arg); //arg.visible(false); } $(document).ready(function() { try { oHandler = $(".mydds").msDropDown().data("dd"); oHandler.visible(true); //alert($.msDropDown.version); //$.msDropDown.create("body select"); $("#ver").html($.msDropDown.version); } catch(e) { alert("Error: "+e.message); } }) </script> </body> </html> I have a list of pictures and I want to control the order. Here is the picture format: $pic = http://www.picture.biz/incoming/w_2GTEK13T961240561_1.jpg $pic = http://www.picture.biz/incoming/w_2GTEK13T961240561_2.jpg $pic = http://www.picture.biz/incoming/w_2GTEK13T961240561_3.jpg $pic = http://www.picture.biz/incoming/w_2GTEK13T961240561_4.jpg $pic = http://www.picture.biz/incoming/w_2GTEK13T961240561_5.jpg Notice the order 1.jpg, 2.jpg, etc.. Here is where I'm stuck... // begin stuck :) $orderPic = explode("_", $pic); foreach($orderPic as $rank){ echo "$rank"; // I need $rank to equal 1 then 2 then 3 ... } It would be nice actually if I could start the insert with number 2 because number one is always a screwy picture. I hope this makes sense / Thank for the help! Completely new to PHP here, so on my website I'd like a specific image to appear according to whether or not the user in my DB has a row called 'premium', with a value of 1. I've tried figuring it out on my own but I'm stuck. I watched a video of someone doing something similar so this is what I have currently, but I'm aware it's totally incorrect. //init.php file <?php if (session_status() == PHP_SESSION_NONE) {//php >5.4 session_start(); } $_SESSION['user_id'] = 1; $db = new PDO('mysql:host=127.0.0.1;dbname=loginsystem', 'root', ''); $userQuery = $db->prepare(" SELECT idUsers, uidUsers, emailUsers, pwdUsers, premium FROM users WHERE idUsers =:user_id "); $userQuery->execute(['user_id' => $_SESSION['user_id']]); $user = $userQuery->fetchObject();
//webpage in which the image will appear <?php require_once '../includes/init.php'; ?> <?php if($user-> premium): ?> <img src="../../img/premium.png" alt="Swiftify+"></a> <?php else: ?> <img src="../../img/swiftify.png" alt="Swiftify+"></a> <?php endif; ?>
Scenario: My page generates several PHP variables. Some of those variables get posted to another page. Question: If a variable is to be assigned to a Session, is there any reason to assign this value to a regular PHP variable? Example: When an API generates a "shipping label tracking number, which is better, "A" or "B" or subtle variation "C":
A.) ... and then use $_SESSION["trackingNumber"] throughout the rest of my script (like this: echo "Hello, here's your tracking number: ".$_SESSION["trackingNumber"]).
B.) ... and then use "$trackingNumber" throughout the rest of my script (like this: echo "Hello, here's your tracking number: ".$trackingNumber).
C.) ... and then use "$trackingNumber" throughout the rest of my script (like this: echo "Hello, here's your tracking number: ".$trackingNumber). Thank you! I've always wondered about this. Which is best?
basically, I have one calculation that relies on the value returned by an if statement, but i can;t for the life of me figure it out correctly... Here is what i am trying to accomplish: Here is the code for the IF i need to get the value of if ($loadedMiles < 500) { (500 - $loadedMiles) * .25 * $mainRate; } And here is the reason why i need it: if ($loadSize == "Full") { $loadedmiles + thevalueoftheif; } basically i will need to have a way to get the values of the functions in the IF statements if that makes any sense... thanks in advance! Why doesn't this code work... Code: [Select] // Initialize variables. $form_value = ''; $form_value = $_POST['form_value']; I get this error... Quote Notice: Undefined index: form_value Thanks, Debbie Running a simple query: SELECT team, rank from table ORDER BY rank ASC returns 8 records in order from 1 to 8 I then want to a assign a new variable to each that I can use for later queries ie: team with rank 1 = $team1 team with rank 2 = $team2 team with rank 3 = $team3 etc.... Thanks in advance. I created a drop-down menu using a MySQL statement in php for a form. My drop down menu works fine, but I want to assign a default value to it (normal text) the value will never change, thus I do not need to extract the default value from the table, I already know the value. Here is the basic snippet from the script: <?php include("../includes/xxx.php"); $cxn = mysqli_connect($host,$user,$password,$dbname); $query = "SELECT DISTINCT `plant_id` FROM `plant` ORDER BY `plant_id`"; $result = mysqli_query($cxn,$query); while($row = mysqli_fetch_assoc($result)) { extract($row); echo "<option value='$plant_id'>$plant_id</option>\n"; } ?> |