PHP - Formating Output Of An Foreach Cycle
Hello i would like to as a little help on how to proper format an foreach output into an HTML table. I have a small function inside an class, that will retrieve the data of an MySQL table and return the result in 1 array, Outside that function i can get the values displayed i only need to format it to show in an table here's what i have:
function that gets the data Code: [Select] function getfansub(){ $subfanarray = array(); $query = mysql_query("SELECT * FROM anr_fansub WHERE 1"); while($result = mysql_fetch_array($query)){ $subfanarray[] = $result; } return $subfanarray; } Outside the funtion/class Code: [Select] $fansubarray = $mysql->getfansub(); VAR_DUMP( $fansubarray ); echo "<br><br>"; foreach($fansubarray as $name){ echo $name['FansubID'] ." ". $name['Designation'] ." ". $name['Website'] ." ". $name['tracker'] ." ". $name['irc'] ."<br>"; } The problem i have with the formating (i really dont know who i can do it) is when he reaches the IRC link he needs to open and close the td and tr brakets of the table. any help or tip is apreciated, thanks in advance Similar TutorialsHey guys, How can i break just one cycle of a foreach loop. Example: $arr = array('0','1','2'); foreach ($arr as $value){ if($value == 1){ break from this iteration and continue onto next one} echo $value; } So the output should be 02 I use foreach to echo a list of tags come out of an array as Code: [Select] foreach ($array as $tag) { echo "$tag,"; }This produces "tag1,tag2,tag3,". The problem is the last ",". How to tell the foreach loop to avoid including "," after the last sequence? I'm using system() to run NMAP and then display the output but it's one big string, how can I format it to look like what I see in the command line? web app output: Starting Nmap 5.00 ( http://nmap.org ) at 2012-03-21 18:44 CDT Interesting ports on localhost (127.0.0.1): Not shown: 993 closed ports PORT STATE SERVICE 80/tcp open http 111/tcp open rpcbind 139/tcp open netbios-ssn 443/tcp open https 445/tcp open microsoft-ds 631/tcp open ipp 3306/tcp open mysql Nmap done: 1 IP address (1 host up) scanned in 0.11 seconds 0 command line output: Starting Nmap 5.00 ( http://nmap.org ) at 2012-03-21 18:48 CDT Interesting ports on localhost (127.0.0.1): Not shown: 993 closed ports PORT STATE SERVICE 80/tcp open http 111/tcp open rpcbind 139/tcp open netbios-ssn 443/tcp open https 445/tcp open microsoft-ds 631/tcp open ipp 3306/tcp open mysql Nmap done: 1 IP address (1 host up) scanned in 0.45 seconds Code: [Select] <?php // path to NMAP $Path = '/usr/bin/nmap'; if(isset($_POST['scan'])) { $IP = $_POST['IP']; $ScanType = $_POST['ScanType']; echo "<br /><u>Results for NMAP Scan</u> <br />"; system("$Path $ScanType $IP 2>&1", $Output); echo $Output; } ?> any help would be greatly appreciated, thanks in advance!! I'm learning PHP and put to myself to a task. But I can't figure this out. It's about the foreach There is this database called books with columns: isbn, author,title, price. The DB has records. I want to output certain records of certain columns:for example echo $books['title"];. I also would like to use some functions I used the following code to titles listing for example: function db_connect() { $result = new mysqli('localhost', 'root', 'password', 'books'); if (!$result) { return false; } $result->autocommit(TRUE); return $result; } function get_book_details($isbn) { // query database for all details for a particular book if ((!$isbn) || ($isbn=='')) { return false; } $conn = db_connect(); $query = "select * from books where isbn='".$isbn."'"; $result = @$conn->query($query); if (!$result) { return false; } $result = @$result->fetch_assoc(); return $result; } $book = get_book_details($isbn); foreach($bookdetail as $isbn => $qty){ echo $book['title']; } but with no luck it gives this error: Warning: Invalid argument supplied for foreach() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\lessons\basics\arrays\foreach.php on line 41 Any help would be appreciated. Hi! This bit of code does exactly what I want it to just not sure why. The way I would (***_u_me) this code to be written would be to replace the echo ($array_2[$index]); with echo ($array_2[$value]); to get the output I'm getting. Basically the foreach loop loops through $array_1 when it finds a key with a null value it outputs the $value or the $index which ever I decide to output of $array_2 which corresponds to that value. when I echo ($array_2[$value]) I get no output and when I echo ($array_2[$index]) I get the wanted output below. It doesn't seem right <?php function test() { $array_1 = array("Matt" => NULL, "Kim" => 1, "Jessica" => NULL, "Keri" => 1); $array_2 = array("Matt","Kim","Jessica","Keri"); foreach ($array_2 as $index => $value) { if (!isset($array_1[$value])) { echo ($array_2[$index]); echo "<br />"; } } } test(); ?> output Matt Jessica Thanks, steadythecourse hello I am trying to print a query out into a table that is grouped and styled by date. If the same date appears I want to style the row similar. Here is what I have: <table class="signup-sched"> <tr><th>signup date</th><th>Name</th><th>Notes</th></tr> <?php $myrows = $wpdb->get_results("SELECT `datefield-256` as signupdate, `your-name` as name, `your-email` as email, `your-message` as notes FROM wp_cc3_signup WHERE DATE_FORMAT(`datefield-256`, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m') ORDER BY `datefield-256`"); $sd = ''; if(count($myrows)){ foreach($myrows as $m){ if ($m->signupdate != $sd){ if ($sd != ''){ echo '<tr class="b"><td>'.$m->signupdate.'</td><td>'.$m->name.'</td><td>'.$m->notes.'</td></tr>'; } echo '<tr class="n"><td>'.$m->signupdate.'</td><td>'.$m->name.'</td><td>'.$m->notes.'</td></tr>'; $sd = $m->signupdate; } } } ?> </table> What am I doing wrong? Hello everyone i have a quick question to ask if i may. On my website i have a presentation video on my homepage, with another 1 just made. What i want to do is when people view my website they see the first video as usual but after that finishes i would like it to switch to the next video which will then replace the first video is that possible in php? I have developed a calendar app. You can add events to certain days, edit, delete them, etc. It works fine now but I'm trying to improve the adding event section. I would like to be able to have a form where there are "name_of_event" and "description" inputs and then display a input that would have a date-picker. I also want to have a link/button that says something to the effect of "add additional day" and then tie a javascript function to it so when it is clicked another date-picker input will pop up under the first, and they'll be able to add as many additional days as they like. I can't figure out how to write the php code that will cycle through only the date-picker inputs though. Something like the following Code: [Select] <?php $name = $_POST['name_of_event']; $description = $_POST['description']; foreach(datepicker_inputs as $date)// <-- part i need help with { mysql_query("INSERT INTO Events (name, description, event_date) VALUES ($name, $description, $date)"); } ?> anybody have ideas on how i can only cycle through the date inputs? I'm pretty sure i'm going to have the date input form name something like "date_1", "date_2", etc. 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. This topic has been moved to Application Frameworks. http://www.phpfreaks.com/forums/index.php?topic=355772.0 I'm trying to figure out how to format a variable. I am listing the contents of a directory from an array and I trying to format the date field to be like - Jan 07, 2011 01:35 PM. When I try this format I get a 500 error message. <td width="'.$width_of_dates_column.'">'.$files("M d Y h:i:s A", filemtime['date'])[$key].'</td></tr>'; Is is possible to format the date after it's been put into the array or does it have to be done while it's being processed. Not sure if you need more code to determine the formatting. Please let me know if you do. Thank you. I have created an email template for HTML emails, but something isn't quite right. I use roundcube for my webmail and the email shows up perfect, but then in outlook it looks terrible. Gmail wont even receive it and in godaddy's webmail it is displayed at text with the <table> code written out. Any idea what I am missing? // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'To: <[mail]>' . "\r\n"; $headers .= 'From: cjkjckjxc <noreply@cojch.com>' . "\r\n"; $body = <<<BODY //stuff goes here BODY; Hi! I'm working a on wordpress website with member pages. Members enter their information via custom fields. Everything is working great except when trying to create a link based on one of those custom field. Unless the member enter his website address in the http://mysite.com format the link my code create is not valid. This is the code i'm currently using Code: [Select] <?php $msite = get_post_meta($post->ID, 'ccio_site', true) ; if (!empty($msite)) { ?><li>Site Internet: <a href="<?php echo ($msite) ; ?>"><?php echo ($msite) ; ?></a></li><?php } ?> Example of a member page where the format create an error. The input field is: Code: [Select] www.pechabot.com Example of a member page where the format is ok. The input field is Code: [Select] http://www.annuairesgb.com How can I format the input string so my code will work even if someone doesn't write the full address with the http:? Thanks a lot for looking So basically I have text which appears on an image. Code: [Select] $ability = "$title gets ability"; //Create Ability imagettfbbox(30, 0, $font, $ability); imagettftext($image, 30, 0, 70, 700, $fontcolor, $font, stripslashes($ability)); But the line of text is longer than the width of the area on the image it needs to fit in. How can I change this so it automatically starts a new line once it reaches the end of said area? Hi All, not able to understand I'm making miskate in CSS or PHP foreach statement. Need help with proper formating of the below code. I want to format the so that seasons divs will contain all the details about the episodes, title & plot. As of now season div is only wraping around the first episode only & ignoring remaining episodes details. Sorry I'm not able to explain this in words but if you access below code you will be able to understand. Code: [Select] <html> <head> <style type="text/css"> .main { width: 750px; margin-right: auto; margin-left: auto; background: #cc1; clear: both; overflow:auto; } .season { margin: 5px; padding: 5px; width: 730px; background: #960; } .details { overflow:hidden; } .ep, .title, .airdate, .plot { margin: 5px; padding: 5px; width: 675px; background: #CCC; } </style> </head> <body> <?php $eps = array( array( 'year' => '2005', 'eptitle' => 'title1', 'airdate' => 'Apr 24 2009', 'epplot' => 'plot of S01E01', 'season' => '1', 'episode' => '1'), array( 'year' => '2005', 'eptitle' => 'title2', 'airdate' => 'May 1 2009', 'epplot' => 'plot of S01E02', 'season' => '1', 'episode' => '2'), array( 'year' => '2005', 'eptitle' => 'title3', 'airdate' => 'May 8 2009', 'epplot' => 'plot of S02E01', 'season' => '2', 'episode' => '1'), array( 'year' => '2005', 'eptitle' => 'title4', 'airdate' => 'May 12 2009', 'epplot' => 'plot of S02E02', 'season' => '2', 'episode' => '2'), array( 'year' => '2005', 'eptitle' => 'title5', 'airdate' => 'May 15 2009', 'epplot' => 'plot of S03E01', 'season' => '3', 'episode' => '1'), array( 'year' => '2005', 'eptitle' => 'title6', 'airdate' => 'May 28 2009', 'epplot' => 'plot of S03E02', 'season' => '3', 'episode' => '2'), ); //var_dump($eps); $season = 0; echo "<div class='main'>"; foreach($eps as $k){ // season check start if($season!=$k['season']) { echo "<div class='season'><b>SEASON : </b>".$k['season']; } //season check finish echo "<div class='details'>"; echo "<div class='ep'><b>EPISODE : </b>".$k['episode']."</div>"; echo "<div class='title'><b>TITLE : </b>".$k['eptitle']."</div>"; echo "<div class='airdate'><b>AIRDATE : </b>".$k['airdate']."</div>"; echo "<div class='plot'><b>PLOT : </b>".$k['epplot']."</div>"; echo "</div>"; // season check start if($season!=$k['season']) { echo "</div>"; $season = $k['season']; } } echo "</div>"; ?> </body> </html> Hi Folks, I understand the basic math to accomplish what I want, at least I think I do I would like to confirm my logic and I need help with the final output format. I am coding a simple browser based, resource type of game. The gamer will select a certain item to produce another item and it will take x time to do it. Here is the basics of what I want, minus the coding to make it look pretty. How do I format $y to show HHH:MM:SS? Code: [Select] $complete = time() + $x; // To show how much time is remaining until completion or complete $y = $complete - time(); if $y > 0 { echo $y; } else { echo "complete"; } Looking for ideas and advise on this one. I want to take a 10 digit number and 1234567890 and format it like 132-456-7890 what would be my best bet as choice of methods to do this? how to format only one specific row based on the value of one cell in a row? $result = mysql_query(select bal, etc. etc ..query ) while($row = mysql_fetch_assoc($result)) echo "<tr>"; echo "<td>" . $row['Account'] . "</td>"; echo "<td>" . $row['licence'] . "</td>"; echo "<td>" . $row['own'] . "</td>"; echo "<td>" . $row['bal'] . "</td>"; echo "</tr>"; Would appreciate help on how to format only the $row['bal'] output to red colour IF bal >5000 Thanks Hey guys & gals, i have the following code: Code: [Select] <?php $date= $_SESSION['date']; echo $date; ?> Which is an echo of an earlier page: Code: [Select] <?php $_SESSION['date'] = date("c")?>; How do i format the echo of the first quote code? At present it echo's like so... Quote 2011-03-30T15:43:43+01:00 ...which although is useful for back office purposes, not so easy on the eye for the customer. How can i format so it's like DATE-MONTH-YEAR? I have googled this and there are examples, but none for when echoing a $_SESSION which is where i'm falling flat on my face. I tried this: but it doesn't work :-( Code: [Select] echo $startdate ("F j, Y") Thanks in advance to all who read and reply How to output a MySQL timestamp field to correct local time with daylight saving with PHP?
Local time is set in php.ini with: date.timezone = "Europe/London"
Confirmed set with: <?php echo date_default_timezone_get(); ?>
But when I echo the timestamp field it does not display correct UK time for the record (an hour behind correct UK time).
<?php echo date('jS F Y - g:ia', strtotime($row['Date'])); ?>
|