List in php expandible - javascript

I have this code that you add as a list, I want only 3 elements to be shown and make the rest appear by clicking on the * show more * button.
$printstring .= '<ol>';
foreach ($rows as $bid) {
$printstring .= '<li>';
if ($bid->bidder_url != "" && $customcontact = "") {
$printstring .= ''.$bid->bidder_name.'';
} else {
$printstring .= $bid->bidder_name;
}
$printstring .= ' bid '.$currencysymbol.number_format($bid->current_bid_price, 2, '.', ',').' on '.get_date_from_gmt($bid->date);
//if ($bid->bid_type == "auto") $printstring .= ' [auto]';
$printstring .= '</li>';
}
$printstring .= '</ol>';
I found that it can be done with js but I don't see how to join both (php and js).
$(function() {
var numToShow = 3;
var moreLi;
$('ul').children('li').hide().each(function() {
if ($(this).index() < numToShow) {
$(this).show();
}
}).parent('ul').append('<li>More</li>');
$('#showMore').click(function() {
moreLi = $(this).parent('li');
moreLi.siblings().show();
moreLi.remove();
});
});

You have to understand that PHP works in backend (in the webserver) and JS works in frontend (in your web browser). Basically, JS should send a request (you could learn about sending requests with Ajax) to PHP, and PHP should return a response to be printed by JS.
If you are new in this kind of scenario, it might be confusing at first. I encourage you to learn about how to send a request with Javascript and parse the response :)

Make a for($i = 0;$i...... then when $i= 4 echo 'Show more...';break;
It should work

Related

when I click this product it does not do anything

I tried to show products if it's available ok added to cart else it show me an alert. if the main window is opened without use AJAX it's works fine but when I **
** for Sorting Item when I use sortby(AJAX) if the item available ok it works add to cart link but if the product is not available when I click it stuck which not do anything, I think the problem with the onClick="*****" but I can't solve it.
I tried many times but I can't solve it.
Thanks for help this is my code.
if (isset($q) || isset($p)) {
$sql="SELECT * FROM product WHERE cat='3'".$q.$p;
$output = '';
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_assoc($result)) {
$output .= '<img src="'.get_image($row['pro_image']).'">';
if ($row['product_status']=='1') {
$output .= '<span>Add to Cart</span>';
} else {
$output .= '<a onclick="return alert("Out of stock!")"><span>Add to Carth</span></a>';
}
} // end while
echo $output;
}

Php and ajax not returning any data from database, no errors

I have created a search using ajax and php, however when I click the search button nothing happens. There are no errors, however when I look at the network in the console it doesn't seem to be reaching searching.php as this file doesn't show up in the network. I want to be able to put the returned data from the search into seperate divs, although at the minute it isn't returning any data at all and I have no idea why.
This is my php
require_once('config1.php');
$output = '';
if(isset($_POST['search']) === true && empty($_POST['search']) === false){
$query = "SELECT a.attraction_name, a.lat, a.long, a.cost, a.image FROM zz_attractions a INNER JOIN zz_city c ON a.city_id = c.city_id WHERE c.city_name = '" . mysqli_real_escape_string(trim($_POST['search'])) . "' ";
$result = mysqli_query($conn, $query);
$count = mysqli_num_rows($result);
if ($count == 0) {
$output = 'there was no search results';
} else {
while ($row = mysqli_fetch_array($result)) {
$attraction_name = $row['attraction_name'];
$lat = $row['lat'];
$long = $row['long'];
$cost = $row['cost'];
$image = "<img src='{$row['image']}' height='100' width='100'>";
$output .= '<div>'.$attraction_name.' '.$lat.' '.$long.' '.$cost.' '.$image.'</div>';
}
}
}
This is my ajax
$('#submit').on('click', function() {
var search = $('#search').val();
if ($.trim(search) != '') { //if search is not equal to nothing - using trim allows users to type in blank space and it won't return anything
$.post('searching.php', {search: search}, function(data) {
$('#return').text(data);
});
}
});
Within the html the div id is #return. If someone could help me find out why no data is returning that would be great.

How to change js code to php

Currently I am learning Php. For an Ebay shop I try to get my page free of JS and to switch to pure CSS via PHP.
For this I have so far a backend, into which I enter my links and texts. These are stored in my menu.js.
$remote_directory = UPLOAD_PATH;
$renaming_file = $remote_directory . '/js/menu.js';
$renaming_file_to = $remote_directory . '/js/menu.old';
$_remc = new Admin_Remote_Copy(ADMIN_UPLOAD_SERVER_HOST, ADMIN_UPLOAD_USER_NAME, ADMIN_UPLOAD_PASSWORD, ADMIN_UPLOAD_SSH_PORT);
$_remc->remote_copy($renaming_file, $renaming_file_to);
if ($_remc->remote_is_writeable($renaming_file)) {
$menu_string = "document.getElementById('categories_menu').innerHTML='";
for ($z = 0; $z < count($_POST['menu_lvl1_text']); $z++) {
$menu_string .= '<li>' . htmlentities($_POST['menu_lvl1_text'][$z], ENT_QUOTES, "UTF-8") . '';
if (isset($_POST['menu' . $z . '_lvl2_text']) && !empty($_POST['menu' . $z . '_lvl2_text'])) {
$menu_string .= '<ul>';
for ($zs = 0; $zs < count($_POST['menu' . $z . '_lvl2_text']); $zs++) {
$menu_string .= '<li>' . htmlentities($_POST['menu' . $z . '_lvl2_text'][$zs], ENT_QUOTES, "UTF-8") . '';
}
$menu_string .= '</ul>';
}
$menu_string .= '</li>';
}
$menu_string .= "';";
$_remc->remote_content_copy($renaming_file, $menu_string);
$local_renaming_file = ADMIN_LOCAL_PATH . '/js/menu.js';
$_remc->local_content_copy($local_renaming_file, $menu_string);
} else {
$file_error = true;
}
Now I had thought to pass these values ​​into an array and save it via JSON PrittyPrint. However, I have no idea at all how to build this array.
My idea is to build a navigation with fixed IDs and fill it with the JSON file.
In the latter I see no problems, but the array is not clear to me.
Let's assume that array would look something like this:
$m =array(
"10100" => array('menu_lvl1_text' => 'menu_lvl1_link'),
"10101" => array('menu_lvl2_text' => 'menu_lvl2_link'),
"10102" => array('menu_lvl2_text' => 'menu_lvl2_link'),
"10103" => array('menu_lvl2_text' => 'menu_lvl2_link')
);
Am I on the wrong path?
And how does my array know that it is to assign value 1 to ID 10000?
Thanks,
Patrick
I think that you forgot to open a folder first! You can use some php functions like opendir at first then get or create a new file there.

Putting a javascript code into a MySQL database

I'm editing a web program in MySQL; unfortunately, I got stuck with the following:
A smarty markup, used to display a form, has the following value:
{php}
$format = $this->get_template_vars("phone_number_format");
$values = explode("|", $this->get_template_vars("VALUE"));
$name = $this->get_template_vars("NAME");
$pieces = preg_split("/(x+)/", $format, 0, PREG_SPLIT_DELIM_CAPTURE);
$counter = 1;
foreach ($pieces as $piece)
{
if (strlen($piece) == 0)
continue;
if ($piece[0] == "x") {
$size = strlen($piece);
$value = (isset($values[$counter-1])) ? $values[$counter-1] : "";
$value = htmlspecialchars($value);
echo "<input type=\"text\" name=\"{$name}_$counter\" value=\"$value\"
size=\"$size\" maxlength=\"$size\" class=\"VOLGENDE\"/>";
$counter++;
} else {
echo $piece;
}
}
{/php}
{if $comments}
<div class="cf_field_comments">{$comments}</div>
{/if}
I need to add some javascript to this, but I cannot manage to get it working. This is the javascript that needs to be added:
$(".VOLGENDE").keyup(function () {
if (this.value.length == this.maxLength) {
$(this).next('.VOLGENDE').focus();
}
});
With this script, I am trying to make sure that the focus will be put on the next field once the maximum amount of characters in a given field is reached.
I am looking for a way to include this javascript into the MySQL database field where the PHP code mentioned above is found. If there is anybody who could help me out with this, then that would be great :)
Thanks in advance!

Trouble deleting row of data from array. PHP + Javascript

I have a screen like this:
notifications example
And it deletes the row, but only from the screen. Because if I refresh then it appears back again. I am not sure how to delete it from the actual array.
The array is taken out of a csv file- and I know how to add it back in etc. But what I don't know is deleting rows from the array.
Heres what I have:
// Grabs the csv file (and its existing data) and makes it into an array so the new data can be added to it.
$Notifications = array();
$lines = file('data/AdminNotifications.csv', FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $value)
{
$Notifications[$key] = str_getcsv($value);
}
echo array2table(array_reverse($Notifications));
// FUNCTION ---------------------------------------------------------------------
//This converts an array to a table
function array2table($array, $recursive = false, $null = ' ')
{
// Sanity check
if (empty($array) || !is_array($array)) {
return false;
}
if (!isset($array[0]) || !is_array($array[0])) {
$array = array($array);
}
// Start the table
$table = "<table>\n";
// The header
$table .= "\t<tr>";
// Take the keys from the first row as the headings
foreach (array_keys($array[0]) as $heading) {
}
$table .= "</tr>\n";
// The body
foreach ($array as $row) {
$table .= "\t<tr>" ;
foreach ($row as $cell) {
$table .= '<td>';
/*
if($cell ==0 && $heading==1){
$cell = $cell.": ";
}
*/
$details = $cell;
// Cast objects
if (is_object($cell)) { $cell = (array) $cell; }
if ($recursive === true && is_array($cell) && !empty($cell)) {
// Recursive mode
$table .= "\n" . array2table($cell, true, true) . "\n";
} else {
$table .= (strlen($cell) > 0) ?
htmlspecialchars((string) $cell) :
$null;
}
$table .= '</td>';
}
$table .= '<td>';
$table .= '<input type="submit" value="Delete" onclick="deleteRow(this)" name="delete"/>';
$table .= '</td>';
$table .= "</tr>\n";
}
$table .= '</table>';
return $table;
}
//If the delete button is pressed, then it does this.
if (isset($_POST['delete'])) {
}
?>
//What happens when it is pressed. (This is javascript)
<script>
function deleteRow(btn) {
var row = btn.parentNode.parentNode;
row.parentNode.removeChild(row);
}
</script>
Any help would be greatly appreciated. I am not too sure whether I can delete a row using javascript? Or in php and java...
Thanks
Php being a server side language will execute only on server. When you are pressing delete button here a javascript function is being called which is actually deleting your row just from client side as it's a client side language that means it actually exist yet on server in that file that's why when you refresh it shows again.
Now you can make an ajax call or place that delete button inside a form to post that request to server to actually delete that.
//If the delete button is pressed, then it does this.
if (isset($_POST['delete'])) {
$arr_index = $_POST['delete']; //you need to pass the row number
unset($array[$arr_index]);
}

Categories