I'm having the problem running php script right when the page loads, and the output is then displayed on the page, which I dont actually want to display. I tried to hide echo with.
<div style='display:none>
but, still doesn't work. I have an idea that "Ajax and JSON Encode function()" will help me to solve this issue of 'making ajax request to same page'. But, I real don't know how to create an array to encode html and php variable that are responsible for the results. Help please.
Here's my ajax function to show the output.
function mass()
{
$.ajax({
url: "page.php",
cache: false,
success: function(html){
$("#container").html(html);
}
});
}
Here's my php script that hold the output of the page
$query="SELECT * FROM `comments` ORDER BY id ASC";
$result = mysql_query($query);
if (isset($_REQUEST['AnswerId'])){
$AnswerId = $_REQUEST['AnswerId'];
}
else {
$AnswerId = 0;
}
$i=0;
while ($mytablerow = mysql_fetch_row($result)) {
$mytable[$i] = $mytablerow;
$i++;
}
function tree($treeArray, $level, $pid = 0) {
global $AnswerId;
if (! $treeArray) {
return;
}
foreach($treeArray as $item) {
if ($item[1] == $pid) {
?>
<div class="Div" style="margin-left:<?php echo($level*60);?>px">
<div class="CotDiv">
<div class="ser"><?php echo($item[2]) ; ?></div>
<div class="Mse"><?php echo($item[3]) ; ?></div>
<div class="ito"><?php echo($item[4]) ; ?></div>
<?php
if ($level<=40) {
echo 'Reply'; }
echo 'Delete';
?> </div> <?php
if ($AnswerId == $item[0]){?>
<div id="InnerDiv"><?php ShowForm($AnswerId);?</div><?php ?> </div><?php
echo ('<br/>');
tree($treeArray, $level+1, $item[0]);
}
}
}
tree($mytable, 0);
?>
#enance this isn't the exact same code, but the idea is the same. This probably doesn't output correctly because I don't have an example array of the mysql output to test.
Just a few notes, try not to use globals and just pass the variable into the function. For the sake of time, I kept the output of html within this function, but ideally you'll like to output html through another function.
<?php
// example array
$mytable = [
[1,0,3,4,5,6],
[0,2,3,4,5,6]
];
$answer_id = 1;
function tree($answer_id, $items, $level, $pid = 0) {
if (empty($array))
return;
foreach ($items as $item) {
if ($item[1] == $pid) {
echo sprintf('<div class="Div" style="margin-left:%spx">',$level * 60);
echo '<div class="CotDiv">';
echo sprintf('<div class="ser">%s</div>', $item[2]);
}
if ($level <= 40) {
echo sprintf('Reply', $item[0]);
echo sprintf('Delete', $item[0]);
}
echo '</div>';
if ($answer_id == $item[0]) {
echo sprintf('<div id="InnerDiv">%s</div>', 'FORM OUTPUT HERE');
tree($answer_id, $items, $level++, $item[0]);
}
echo '</div>';
}
}
tree($answer_id, $mytable, 0);
exit;
I really hope this helps to get you on the right track.
Related
I am in the process of building a software download site for my company.
However, I have come across a problem that I am unsure how to get past.
I am retrieving all the information from a table about a particular software release and placing them in a multidimensional array. I am then trying to run a foreach (I have even tried a for loop) against that array and I get an error shown below:
When I run var dump against the original array, I get this:
So I am really confused as I don't know what I'm missing or where I am going wrong. The reason why I want to run this is so that I can filter the array into a one dimensional array.
Below is the code for the main web page
<?php
//Displays list of companies in 2 columns
$versionAccess = VersionAccess::findAccess($relId);//This gets set earlier in the webpage
$count = count($versionAccess);
var_dump($versionAccess);
foreach ($versionAccess as $va)
{
if ($va->company_access != '0')
{
$versionAcc[] = $va->comapny_id;
}
}
foreach ($company as $compAccess)
{
$compAccessId = $compAccess->company_id;
if (in_array($compAccessId, $versionAcc))
{
$access = 'checked disabled';
}
else { $access = 'disabled'; }
$accessName = 'access'.$compAccessId;
if ($ctr % 2 == 0)
{
echo '<td>'.$compAccess->company_name.':</td>';
echo '<td><label class="switch"><input type="checkbox" name="'.$accessName.'" value="1" '.$access.'><span class="slider round"></span></label></td>';
echo '</tr>';
}
else
{
if ($ctr < $compCount)
{
echo '<tr>';
echo '<td>'.$compAccess->company_name.':</td>';
echo '<td><label class="switch"><input type="checkbox" name="'.$accessName.'" value="1" '.$access.'><span class="slider round"></span></label></td>';
}
else
{
echo '<tr>';
echo '<td>'.$compAccess->company_name.':</td>';
echo '<td><label class="switch"><input type="checkbox" name="'.$accessName.'" value="1" '.$access.'><span class="slider round"></span></label></td>';
echo '</tr>';
}
}
$ctr++;
}
?>
The function that brings the data from the database is:
public static function findAccess($accessId)
//find the version access in database
{
return self::findQuery("SELECT * FROM version_access WHERE version_id = '$accessId'");
}
The findQuery method:
public static function findQuery($sql)
{
global $database;
$resultSet = $database->query($sql);
$objectArray = array();
while ($row = mysqli_fetch_array($resultSet))
{
$objectArray[] = self::instant($row);
}
return $objectArray;
}
I am still relatively new and any help is greatly appreciated.
I was wondering if it's possible to have an if statement within an echo.
I have if statement which works fine when echoing results through the a while loop... This is the statement:
<div><?php if ($row['image'] == '') {}
else {echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";} ?>
<?php if ($row['video'] == '') {}
else {echo "<iframe src={$row['video']}></iframe>";} ?></div>`
So basically it's either a video or an image which works fine but then I implemented an infinite scroll to my blog which echoes the data from the database through and if statement like so:
if ($results) {
while($obj = $results->fetch_object())
{
echo '
<div><h3>'.$obj->headline.'</h3> </div>
<div><img src='data:image/jpeg;base64,".base64_encode('.$obj->image.')."'></div>'
So I wondering if anyone knows if it's possible to transfer that if statement within this echo so that it display an image firstly and then knows whether one is present or when a video is present within the database.
Thanks in advance for any help.
PS: I'm very new to coding/php!
Of course. Just split up the echo into multiple statements:
while($row = $results->fetch_object()) {
echo '<div>';
if ($row['image'] == '') {
} else {
echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";
}
if ($row['video'] == '') {
} else {
echo "<iframe src={$row['video']}></iframe>";
}
echo '</div>';
}
Try this one.
//first initialize a variable as a string
$result="";
while($obj = $results->fetch_object()) {
$result.="<div>";
if (!empty($obj['image'])){
$result.="<img src='data:image/jpeg;base64,".base64_encode($obj['image'])."'>";
}
elseif (!empty($obj['video'])){
$result.="<iframe src={$obj['video']}></iframe>";
}else{
//show some notification or leave it
//echo 'not Found';
}
$result.="</div>";
}
//finally you need to print the result variable.
echo $result;
Couple of issues here I'm not sure on what the issue is but with how my coding is set up it looks odd in the firebug with how the uls, lis, and h2s are rendered. Any ideas why and also I need some suggestions on how to fix the errors I am getting which is caused by some categories not having child pages. In which case if they don't have child pages I don't want my code to do anything just pass over it.
kansasoutlawwrestling.com/site-map
<?php
echo "<pre>";
print_r($categoriesArray);
echo "</pre>";
if((isset($categoriesArray)) AND ((!empty($categoriesArray))||($categoriesArray !== NULL)))
{
if(count($categoriesArray) <= 0)
{
echo "There are no content pages on this site!";
}
else
{
foreach ($categoriesArray as $row)
{
echo "<h2>".stripslashes($row['name'])."</h2>";
echo "<ul>";
foreach ($row['children'] as $row2)
{
echo "<li>".stripslashes($row2['link_name'])."</li>";
if (count($row2) > 0)
{
echo "<ul>";
foreach ($row2['child_pages'] as $row3)
{
echo "<li>".stripslashes($row3['link_name'])."</li>";
}
echo "<ul>";
}
}
echo "</ul>";
}
}
}
else
{
echo "There are no content pages on this site!";
}
?>
EDIT: Any other ideas for me to try?
You could do;
if(count($row2['child_pages']) < 1) {
continue;
}
Hope it helps
I have this code: messages.php
if (count($row) > 0)
{
foreach ($row as $r)
{
//some code setting variables
if ($opened_once >= 1)
{
}
else
{
echo "<tr>";
echo '<td><a class="red_link" href="'.ADDRESS.'view_message.php?id='.$r['id'].'" id= "subject_id" ><span id = "subject">'.$r['subject'].'</span></a></td>';
echo '<td id = "unique_code1">'.$uniqueCode1.'<span class="pink_text" id = "unique_code2">'.$uniqueCode2.'</span><span id = "unique_code3">'.$uniqueCode3.'</span></td>';
echo "</tr>";
}
}
I need to update $r['id'], $r['subject'], $uniqueCode1, $uniqueCode2, $uniqueCode
My jQuery code:
<script>
$(document).ready(function()
{
refresh();
});
function refresh()
{
$.post('getMessageDetails.php', function (json) {
$("#subject").html(json.subject);
$("#subject_id").html(json.subject_id);
$("#unique_code1").html(json.unique_code1);
$("#unique_code2").html(json.unique_code2);
$("#unique_code3").html(json.unique_code3);
});
window.setTimeout(refresh,30000);
}
</script>
Then I have newMessageCnt.php
<?php
<?php
header('Content-Type: application/json; charset=utf-8');
include('header_application.php');
$limit = 15;
if(!isset($_GET['page']))
$page = 1;
else
$page = $_GET['page'];
$from = (($page * $limit) - $limit);
$row = $obj_clean->getMessages($_SESSION['user_id'], $from, $limit);
if (count($row) > 0)
{
foreach ($row as $r)
{
$codeLength = strlen($r['unique_code']);
$codeLength = strlen($r['unique_code']);
$firstPartLength = $codeLength - 5;
$uniqueCode3 = substr($r['unique_code'], -2);
$uniqueCode2 = substr($r['unique_code'], -5, 3);
$uniqueCode1 = substr($r['unique_code'], 0, $firstPartLength);
$message_id = $r['id'];
$subject = $obj_clean->getMessageDetails($message_id);
$opened_once = $obj_clean->getOpenedOnce($message_id);
if ($opened_once >= 1)
{
$array['subject'] = $r['subject'];
$array['subject_id'] = $r['id'];
$array['unique_code1'] = $uniqueCode1;
$array['unique_code2'] = $uniqueCode2;
$array['unique_code3'] = $uniqueCode3;
}
}
}
echo json_encode($array);
exit();
?>
?>
I call this .php somewhere else as well where I just want the value of echo $obj_clean->getUnopenedMessagesCount($_SESSION['user_id']);
But from my messages.php I want
echo '<td><a class="blue_link" href="'.ADDRESS.'view_message.php?id='.$r['id'].'">'.$r['subject'].'</a></td>';
echo '<td>'.$uniqueCode1.'<span class="pink_text">'.$uniqueCode2.'</span>'.$uniqueCode3.'</td>';
I'm not sure if I'm doing this right, some advice please?
$.post('ajax_call.php', function (json) {
$("#subject").html(json.subject);
$("#unique_code").html(json.unique_code);
});
//ajax_call.php
<?php
$array['subject']='bla-bla';
$array['unique_code']='1231312';
header('Content-Type: application/json; charset=utf-8');
echo json_encode($array);
exit();
Better would be to have your PHP return an object containing the values you want as properties. This can be sent back to the browser using JSON. Your client-side code see this as an object and can extract the properties and populate the HTML. This separates your presentation (VIEW) from your data (MODEL).
EDIT: Exactly as #TROODON has answered.
I have many goals to be printed on the screen.
but it shows error when i use it like this
echo $this->validation->rshort_goal.$i;
What is the right way to use this?
if($sgoal !='')
{
$scount = count($sgoal);
$i =1;
foreach($sgoal as $row)
{
<textarea name="rshort_goal<?php print $i;?>" id="short_goal" class="short_go">
<?php if($this->validation->rshort_goal.$i)
{
echo $this->validation->rshort_goal.$i;
}
elseif($this->validation->rshort_goal.$i._error !='')
{ echo ''; }
else
{echo $$row->goal_description; }
?>
</textarea>
<?php
$i++;
}
}
echo #$this->validation->{'rshort_goal'.$i};
Perhaps you want to call a function like this?
call_user_func($this->validation, 'rshort_goal' . $i);