So, I have a select option HTML where the data is got from json_decode(file_get_contents("myAPI.com")) and it looked like this:
note : the - is how I divide each data manually.
As you can see, the result is not neat, so tight, and hard to read.
I've tried using Javascript like this :
var spacesToAdd = 5;
var biggestLength = 0;
$("#pks_bank option").each(function(){ var len = $(this).text().length;
if(len > biggestLength){
biggestLength = len;
} }); var padLength = biggestLength + spacesToAdd;
$("#pks_bank option").each(function(){
var parts = $(this).text().split('+');
var strLength = parts[0].length;
for(var x=0; x<(padLength-strLength); x++){
parts[0] = parts[0]+' ';
}
$(this).text(parts[0].replace(/ /g, '\u00a0')+'+'+parts[1]).text; });
But the result is getting worse and farther like this :
Below is my select option code :
<select name='pks_bank' id='pks_bank' data-widget='select2' class='multisel'
style='width:65%; padding-bottom:5px;padding-top:5px;'>
<option value='no'>- Pilih PKS Bank - </option>
<?php
foreach ($list_pks_bank as $data) {
$id_wok = $data->PKS1 . '/' . $data->PKS2 . '/' . $data->PKS3 . '/' . $data->PKS4;
$wok = $data->NMBNK . ' - ' . $data->NOPKS1 . ' - ' . $data->NOPKS2;
echo "<option value='" . $data->PKS2 . "'>" . ucwords(strtoupper($wok)) . "</option>";
}
?>
</select>
How do I make a column inside select option in HTML/PHP? So it would be like this :
In .NET I can give the Combo Box Column > ListBoxColumn property
I'm actually trying to create a ( more or less ) instant chat.
There's just one thing left to go, until it's finished. And that's a DB check for new entries every second by PHP itself.
A while loop works great for it, because i can exit it perfectly.
The Problem is, that the "every second" thing isn't working.
I tried a sleep(1), but that causes a 1 minute server freeze, until the script has been completed.
Hope someone can help me, i'm actually frustrated about this problem.
elseif($latestID != 'undefined' && $_POST['returnafter'] == '60')
{
$timeout = '60';
$i = '0';
while ($i != $timeout)
{
$chat_content = "";
$i++;
$getLastID = mysql_query("SELECT id, userid, content, time_posted FROM (SELECT * FROM chat_system ORDER BY id DESC LIMIT 150) chat_system WHERE id > '" . $latestID . "' $extraQuery ORDER BY id ASC");
while ($lastID = mysql_fetch_object($getLastID))
{
$rowID = $lastID->id;
$user_posted_id = $lastID->userid;
$chat_content = $lastID->content;
$chat_posted = $lastID->time_posted;
$getUserData = mysql_query("SELECT username, avatar, loggedIn FROM account_data WHERE account_id=('" . $user_posted_id . "')");
while ($userData = mysql_fetch_object($getUserData))
{
$username = $userData->username;
$useravatar = $userData->avatar;
$loginStatus = $userData->loggedIn;
}
if ($loginStatus == '1')
{
$onlineStatus = '<img src="./images/3DArt/newOnline.png" class="chat_onlineStatus">';
}
else
{
$onlineStatus = '<img src="./images/3DArt/newOffline.png" class="chat_onlineStatus">';
}
if (date('Y-m-d', $chat_posted) == date('Y-m-d'))
{
$time_posted = strftime('Heute, %H:%M', $chat_posted);
}
elseif (date('Y-m-d', $chat_posted) == date('Y-m-d', strtotime("Yesterday")))
{
$time_posted = strftime('Gestern, %H:%M', $chat_posted);
}
elseif (date('Y-m-d', $chat_posted) < date('Y-m-d', strtotime("Yesterday")))
{
$time_posted = strftime("%A, %d %B %Y %H:%M", $chat_posted);
}
if (isset($_POST['parseEmoticons']) && $_POST['parseEmoticons'] == 'true')
{
$chat_content = emoticons($chat_content);
}
$newChatRow.= '<div class="chatRow" id="' . $rowID . '">
<div class="chatRow_container">
<div><img src="' . $useravatar . '" class="chatAvatar">' . $onlineStatus . '<b>' . $username . '</b>
</div>
</div>' . $modActions . '
<div class="chat_mainMsg">
' . $chat_content . '
</div>
<div class="chatTime">' . $time_posted . '</div>
</div>';
}
$content = str_replace(array(
'\r\n',
'\r',
'\n'
) , "<br />", $newChatRow);
if(!empty($chat_content)) { echo $newChatRow; $i = $timeout; return false; }
if(empty($chat_content)) { sleep(1); return true; }
}
}
I think, this informations should be enough. If not, just ask.
EDIT: The request is initialized by Ajax and on success the request repeats.
That's why PHP have to check for 60 seconds.
you should let the clientside decide what messages it "like" to receive.
eg you call your messages.php class - per ajax on the client side and pass a timestamp when the last time was the script requested new messages
example:
ajax interval with 1 second loop call the messages.php?last=123456 and receive all messages that were created after or equal this timestamp. now you update in javascript the last timestamp and so on.
or you use a perfect framework (if possible) that is designed perfectly for your task to solve this.
http://socket.io/ (or js keyword websockets)
I am using the jQuery DataTables plugin to display a set of results combined from multiple tables. I am trying to implement server-side processing through the plugin to improve page load times. I can't quite figure out how to extrapolate the example DataTables gives to my scenario. Any ideas/suggestions?
Javascript:
$('#results').dataTable({
"sAjaxSource": "../server_processing.php",
"bProcessing": true,
"bServerSide": true,
"bDeferRender": false,
})
Original PHP Function:
function build_data_list(){
global $org_id;
global $dbh;
global $req_ids;
global $user_list;
$sth = $dbh->query ("SELECT
l4.name as L4name,
l3.name as L3name,
l2.name as L2name,
l1.name as L1name,
u.id,
u.last_name,
u.first_name,
FROM user_grp_indx ugi, groups l4, groups l3, groups l2, groups l1, users p
WHERE
ugi.user_id = u.id
AND l4.id = ugi.grp_id
AND l4.parent = l3.id
AND l3.parent = l2.id
AND l2.parent = l1.id
ORDER BY u.id", PDO::FETCH_ASSOC);
$row = $sth->fetch();
$item['user_id'] = $row['id'];
$item['user_info'] = '<a href="../users/index.php?pq=';
$item['user_info'] .= $row['id'].'">';
$item['user_info'] .= $row['last_name']. ", " . $row['first_name'] . "</a>";
$item['l1_name'] = $row['L1name'];
$item['l2_name'] = $row['L2name'];
$item['l3_name'] = $row['L3name'];
$item['l4_name'] = $row['L4name'];
for ($i=0; $i < sizeof($req_ids) ; $i++ ){
$item['req'.$i] = (chk_req_status($item['user_id'],$req_ids[$i]) ? "<span title=\"Yes\"></span><img src=\"../../media/icons/tick.png\" alt=\"Yes\" />" :
"<span title=\"No\"></span><img src=\"../../media/icons/cross.png\" alt=\"No\" />");
}
$old_L1id = $row['L1id'];
$old_user_id = $row['id'];
while ($row = $sth->fetch()){
$L1id = $row['L1id'];
$user_id = $row['id'];
if ($L1id == $old_L1id && $user_id == $old_user_id ){
$item['l2_name'] .= "<br/>" . $row['L2name'];
$item['l3_name'] .= "<br/>" . $row['L3name'];
$item['l4_name'] .= "<br/>" . $row['L4name'];
continue;
}
$user_list[] = $item;
$old_L1id = $L1id;
$old_user_id = $user_id;
$item['user_id'] = $row['id'];
$item['user_info'] = '<a href="../users/index.php?pq=';
$item['user_info'] .= $row['id'].'">';
$item['user_info'] .= $row['last_name']. ", " . $row['first_name'] . "</a>";
//add inital level stuff to the new record.
$item['l1_name'] = $row['L1name'];
$item['l2_name'] = $row['L2name'];
$item['l3_name'] = $row['L3name'];
$item['l4_name'] = $row['L4name'];
for ($i=0; $i < sizeof($req_ids) ; $i++ ){
$item['req'.$i] = (chk_req_status($item['user_id'],$req_ids[$i]) ? "<span title=\"Yes\"></span><img src=\"../../media/icons/tick.png\" alt=\"Yes\" />" :
"<span title=\"No\"></span><img src=\"../../media/icons/cross.png\" alt=\"No\" />");
}
}
}
Original HTML/PHP:
<tbody>
<?php foreach($user_list as $item){
print "<tr>";
print "<td class=\"hidden\">{$item['user_id']}</td>";
print "<td>{$item['user_info']}</td>";
print "<td>{$item['l1_name']}</td>";
print "<td>{$item['l2_name']}</td>";
print "<td>{$item['l3_name']}</td>";
print "<td>{$item['l4_name']}</td>";
for ($i=0; $i < sizeof($req_ids) ; $i++ ){
print '<td>'.( chk_req_status($item['user_id'],$req_ids[$i]) ? "<span title=\"Yes\"></span><img src=\"../../media/icons/tick.png\" alt=\"Yes\" />":
"<span title=\"No\"></span><img src=\"../../media/icons/cross.png\" alt=\"No\" />").'</td>';
}
print "</tr>";
} ?>
</tbody>
Can this be of any help?
Server-side processing | PHP with MySQL
Server-side processing | PHP with MySQL
This script serves as the basis for the testing and development that happens with DataTables, so it is always right up to date, and will always implement all of the features that are supported in DataTables with server-side processing (with the exception of regex filtering - for database access speed reasons).
To use the code on your own server, simply change the $aColumns array to list the columns you wish to include from your database, set $sIndexColumn to a column which is indexed (for speed), $sTable to the table name, and finally fill in your database connection parameters to $gaSql. Please note that this script uses json_encode which requires PHP 5.2 or newer. For a version of this script which is compatible with older versions please see the PHP 4 compatible version.
My problem is:
I'm trying to submit an array of hidden input types, which are stacked into an array using jquery onclick, to a PHP file. However, when I try to count or even echo the passed variable in the php file (saveTest.php), no data appears or the count variable is zero.
I've searched and I found this guy's question:
pass an array from jQuery to PHP (and actually go to the page after submit)
I think I'm close to the above post but I'm still a newbie in jQuery so I don't understand much of the codes.
This is my jquery:
$(function(){
$("td").click(function(){
if($(this).hasClass("on"))
{
alert("Already marked absent");
}
else
{
$(this).addClass("on");
var currentCellText = $(this).text();
$("#collect").append("<input type='text' hidden = '" + currentCellText + "'/>" + currentCellText);
}
});
$("#clicky").click(function(){
$("td").removeClass("on");
$("#collect").text('');
$("#collect").append("Absentees: <br>")
});
});
<?php
session_start();
include 'connectdb.php';
$classID = $_SESSION['csID'];
$classQry = "SELECT e.csID, c.subjCode, c.section, b.subj_name, e.studentID, CONCAT(s.lname, ', ' , s.fname)name
FROM ENROLLMENT e, CLASS_SCHEDULE c, STUDENT s, SUBJECT b
WHERE e.csID = c.csID
AND c.csID = '" . $classID . "'
AND c.subjCode = b.subjCode
AND e.studentID = s.studentID
ORDER BY e.sort;";
$doClassQry = mysql_query($classQry);
echo "<table id='tableone'>";
while($x = mysql_fetch_array($doClassQry))
{
$subject = $x['subj_name'];
$subjCode = $x['subjCode'];
$section = $x['section'];
$studentArr[] = $x['name'];
$studentID[] = $x['studentID'];
}
echo "<thead>";
echo "<tr><th colspan = 7>" . "This is your class: " . $subjCode . " " . $section . " : " . $subject . "</th></tr>";
echo "</thead>";
echo "<tbody>";
echo "<tr>";
for($i = 0; $i < mysql_num_rows($doClassQry); $i++)
{
if($i % 7 == 0)
{
echo "</tr><tr><td id = '". $studentID[$i] . " '>" . $studentArr[$i] . "</td>";
}
else
{
echo "<td id = '". $studentID[$i] . " '>" . $studentArr[$i] . "</td>";
}
}
echo "</tr>";
echo "</tbody>";
echo "</table>";
?>
This is my php file (saveTest.php)
<?php
$absent = $_POST['absent'];
//echo "absnt" . $absent[] . "<br>";
echo count($absent);
?>
Add name to hidden field:
$("#collect").append("<input type='hidden' name="absent[] value= '" + currentCellText + "'/>" + currentCellText);
It looks like you want to submit a javascript array to a php script and then make use of it. You can make use of .each() function to loop through all the hidden values and adding them into the array. Then use $.post to submit the array to a php script.
<script src="jquery.js"></script>
<script>
$(function(){
$('#btn_submit').click(function(){
var array_hidden = [];
$('input[type=hidden]').each(function(index){
var current_value = $.trim($(this).val());
array_hidden[index] = current_value;
});
$.post('arraysubmit.php', {'hidden_array' : array_hidden}, function(data){
$('#results').html(data);
});
});
});
</script>
<?php for($x=0; $x<=10; $x++){ ?>
<input type="hidden" name="name[]" value="Name<?php echo $x; ?>">
<?php } ?>
<input type="button" id="btn_submit">
<div id="results"></div>
You can then access the array in the php script using the post variable and do whatever you want with it:
$_POST['hidden_array']
I have a loop of data that will only echo the loop inside the while function, but if i call/echo the looped data outside the while function, it only runs the 1st loop.
SAMPLE:
$num = mysql_num_rows($queryFromDB);
$i=0;
while($i < $num)
{
$field1= mysql_result($queryFromDB,$i,"field1");
$field2= mysql_result($queryFromDB,$i,"field2");
$bothFields = $field1 . " " . $field2 "\n";
// This will show 2 rows of data
echo $bothFields;
$i++;
// This will only show 1 row of data. How can I pass the looped data to another variable?
echo $bothFields;
}
The output that I wanted to show is:
TITLE/HEADER GOES HERE in the 1st Line
-1st Row of Data from DB
-2nd Row of Data from DB
Here's the actual code:
$num = mysql_num_rows($qWoundAssessment);
$i=0;
while ($i < $num)
{
$wndType = mysql_result($qWoundAssessment,$i,"wndType");
$wndNum = mysql_result($qWoundAssessment,$i,"wndNum");
$wndLocation = mysql_result($qWoundAssessment,$i,"wndLocation");
$wndStage = mysql_result($qWoundAssessment,$i,"wndStage");
$wndL = mysql_result($qWoundAssessment,$i,"wndL");
$wndD = mysql_result($qWoundAssessment,$i,"wndD");
$wndW = mysql_result($qWoundAssessment,$i,"wndW");
$wndAseptic = mysql_result($qWoundAssessment,$i,"wndAseptic");
$wndIrrigate = mysql_result($qWoundAssessment,$i,"wndIrrigate");
$wndIrrigateBox = mysql_result($qWoundAssessment,$i,"wndIrrigateBox");
$wndPat = mysql_result($qWoundAssessment,$i,"wndPat");
$wndCover = mysql_result($qWoundAssessment,$i,"wndCover");
$wndCoverBox = mysql_result($qWoundAssessment,$i,"wndCoverBox");
$wndSecure = mysql_result($qWoundAssessment,$i,"wndSecure");
$wndSecureBox = mysql_result($qWoundAssessment,$i,"wndSecureBox");
$wndQvisit = mysql_result($qWoundAssessment,$i,"wndQvisit");
$wnd = "-" . $wndType . " " . "#" . $wndNum . ", " . "LOCATION " . $wndLocation . ", " . "STAGE " . $wndStage;
$wndSize = "SIZE " . $wndL . "CM" . " X " . $wndW . "CM" . " X " . $wndD;
if($wndAseptic=="1"){$wndAsepticTech = "USING ASEPTIC TECHNIQUE";}
if($wndIrrigate=="1"){$wndIrrigateWith = "IRRIGATE WITH " . $wndIrrigateBox;}
if($wndPat=="1"){$wndPatDry = "PAT DRY";}
if($wndCover=="1"){$wndCoverWith = "COVER WITH " . $wndCoverBox;}
if($wndSecure=="1"){$wndSecureWith = "COVER WITH " . $wndSecureBox;}
if($wndQvisit=="1"){$wndQv = "Q VISIT";}
if(isset($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQv)){
$woundCare = implode(", ",array($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQv)) . "\n\n ";}
$wndCare .= $woundCare;
$i++;
}
$snWoundCare = "SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:" . "\n" . $wndCare;
if I echo $wndCare, it shows the "Undefined variable" error with the actual looped data. But if I pass this variable to PDF, it works.
SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:
-PRESSURE ULCER #1, LOCATION COCCYX, 3, SIZE 2.0CM X 1.5CM X 0.07, USING ASEPTIC TECHNIQUE, IRRIGATE WITH NORMAL SALINE, PAT DRY, COVER WITH AQUACEL AG, COVER WITH MEPILEX BORDER, Q VISIT
-SURGICAL WOUND #2, LOCATION (R) KNEE, , SIZE 29CM X 0CM X 0, USING ASEPTIC TECHNIQUE, IRRIGATE WITH NORMAL SALINE, PAT DRY, COVER WITH AQUACEL AG, COVER WITH MEPILEX BORDER, Q VISIT
================ CODE NOW WORKS!!! HERE's MY FINAL SOLUTION ======================
$num = mysql_num_rows($qWoundAssessment);
$i=0;
$storeMyData = array();
while($i < $num)
{
$wnd= "-" . mysql_result($qWoundAssessment,$i,"wndType") . " #" . mysql_result($qWoundAssessment,$i,"wndNum"). ", LOCATION " . mysql_result($qWoundAssessment,$i,"wndLocation") . ", STAGE " . mysql_result($qWoundAssessment,$i,"wndStage");
$wndSize = "SIZE " . mysql_result($qWoundAssessment,$i,"wndL") . "CM" . " X " . mysql_result($qWoundAssessment,$i,"wndW") . "CM" . " X " . mysql_result($qWoundAssessment,$i,"wndD") . "CM";
if(isset($rowWoundAssessment['wndAseptic'])){$wndAsepticTech = "USING ASEPTIC TECHNIQUE";}
if(isset($rowWoundAssessment['wndIrrigate'])){$wndIrrigateWith = "IRRIGATE WITH " . mysql_result($qWoundAssessment,$i,"wndIrrigateBox");}
if(isset($rowWoundAssessment['wndPat'])){$wndPatDry = "PAT DRY";}
if(isset($rowWoundAssessment['wndCover'])){$wndCoverWith = "COVER WITH " . mysql_result($qWoundAssessment,$i,"wndCoverBox");}
if(isset($rowWoundAssessment['wndSecure'])){$wndSecureWith = "SECURE WITH " . mysql_result($qWoundAssessment,$i,"wndSecureBox");}
if(isset($rowWoundAssessment['wndQvisit'])){$wndQvisit = "Q VISIT";}
$wndCare = implode (", ", array($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQvisit)). "\n\n";
// This will show 2 rows of data
$storeMyData[] = $wndCare ; // store current data in array
$i++;
}
/* this will echo your storedData of loop */
foreach($storeMyData as $prevData)
/* or join the data using string concatenation /
$allFinalData2 = "";
/ this will echo your storedData of loop */
foreach($storeMyData as $prevData)
{
$allFinalData2 = $allFinalData2.$prevData ; // keep on concatenating
}
echo "SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:" . "\n" . $allFinalData2;
thanks to DhruvPathak and Antonio Laguna! You guys are the best! Just made my day! jumps around the room
This should work:
<?php
$wndCare = '';
while ($row = mysql_fetch_assoc($qWoundAssessment)){
$wnd = '-'.$row['wndType'].' #'..$row['wndNum'].', LOCATION '.$row['wndLocation'].', STAGE '.$row['wndStage'];
$wndSize = 'SIZE '.$row['wndL'].'CM X '.$row['wndW'].'CM X '.$row['wndD'];
$wndAsepticTech = ($row['wndAseptic'] == 1) ? 'USING ASEPTIC TECHNIQUE' : '';
$wndIrrigateWith = ($row['wndIrrigate'] == 1) ? 'IRRIGATE WITH '.$row['wndIrrigateBox'] : '';
$wndPatDry = ($row['wndPat'] == 1) ? 'PAT DRY' : '';
$wndCoverWith = ($row['wndCover'] == 1) ? 'COVER WITH'.$row['wndCoverBox'] : '';
$wndSecureWith = ($row['wndSecure'] == 1) ? 'COVER WITH'.$row['wndSecureBox'] : '';
$wndSecureWith = ($row['wndSecure'] == 1) ? 'COVER WITH'.$row['wndSecureBox'] : '';
$wndQvisit = ($row['wndQvisit'] == 1) ? 'Q VISIT' : '';
$wndCare .= implode (", ", array($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQv)). '\n\n';
}
$snWoundCare = "SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:" . "\n" . $wndCare;
?>
The issue I see is that you were testing if all variables where previously setted and this could make strange things as you were stablishing them sometimes and sometimes don't.
I am not sure what you want to do with your data. It seems you want to store
all the data to use it outside the loop, then this is the way to go :
<?php
$num = mysql_num_rows($queryFromDB);
$i=0;
$storeMyData = array();
while($i < $num)
{
$field1= mysql_result($queryFromDB,$i,"field1");
$field2= mysql_result($queryFromDB,$i,"field2");
$bothFields = $field1 . " " . $field2 "\n";
// This will show 2 rows of data
echo $bothFields;
$storeMyData[] = $bothFields ; // store current data in array
$i++;
}
/* this will echo your storedData of loop */
foreach($storeMyData as $prevData)
{
echo $prevData."\n";
}
?>
$allFinalData = implode("",$prevData); // implode will join all the data as string
echo $allFinalData."\n" ;
/* or join the data using string concatenation */
$allFinalData2 = "";
/* this will echo your storedData of loop */
foreach($storeMyData as $prevData)
{
$allFinalData2 = $allFinalData2.$prevData ; // keep on concatenating
}
echo $allFinalData2,"\n";
?>