I have the statement if(isset($votes)) in votes_allvotes.tpl.php.
If I check the value on the same line right before that statement it tells me the value is 2, but for some reason the if statement refuses to acknowledge this.
Also tried if($votes > 0), did not work. I use this on 2 pages and on the other one it works fine, though there is nothing on this page that should conflict in any way with this (deleted everything on the page and it still didnt work.
Using Joomla 1.5.21 and Virtuemart 1.1.6
Here is the code:
<div class="rating" style="margin: 0px 0px 15px -9px">
<?php
$product_flypage = $_SERVER['REQUEST_URI'];
echo ps_reviews::allvotes( $product_flypage );
?>
ps_reviews::allvotes:
function allvotes( $product_id ) {
if (strstr($product_id, '&')) {
$arr = explode('&', $product_id);
$pro_arr = explode('=', $arr[2]);
$cat_arr = explode('=', $arr[3]);
$product_id = $pro_arr[1];
$category_id = $cat_arr[1];
}
global $db;
$tpl = new $GLOBALS['VM_THEMECLASS']();
$dbc = new ps_DB;
$q = "SELECT votes, rating FROM #__{vm}_product_votes "
. "WHERE product_id='$product_id' ";
$dbc->query( $q );
$votes = $dbc->f("votes");
$rating = $dbc->f("rating");
if( $votes != 0)
$tpl->set('votes', $votes );
$tpl->set('rating', $rating );
$tpl->set('product_id', $product_id );
$tpl->set('category_id', $category_id );
return $tpl->fetch( 'common/votes_allvotes.tpl.php' );
}
votes_allvotes.tpl.php (WHERE THE ACTUAL PROBLEM IS):
if (isset($votes)) { ?>
<img src="<?php echo VM_THEMEURL ?>images/stars/<?php echo $rating ?>.gif" align="middle" border="0" alt="<?php echo $rating ?> stars" />
<?php echo $VM_LANG->_('PHPSHOP_TOTAL_VOTES').": ". $votes;
}
else {
if (!empty($my->id)) {
if( $_GET['flypage']){
echo "<li class='reviews'><a href='javascript:void(0)' style='color:#000;'><b>Wees de eerste om een recensie te schrijven!</b></a></li>";
}else{
echo "<a href='/index.php?page=shop.product_details&flypage=flypage.tpl&product_id=". $product_id ."&category_id=". $category_id ."&option=com_virtuemart&Itemid=1&lang=nl&review=yes'><b>Wees de eerste om een recensie te schrijven!</b></a>";
}
}else {
echo $VM_LANG->_('PHPSHOP_REVIEW_LOGIN'); // Login to write a review!
}
}
isset might not be returning what you expected.
$votes = 0;
if(isset($votes)){
echo 'Ooer missus ...';
}
I am not sure but perhaps you want ...
if((int)$votes > 0){
echo 'well you got at least one vote ...';
}
Anyhow, if a condition does not do as you expect, then
var_dump()
that variable onto the page or into a log file and inspect it.
Even better, inspect it PRIOR to writing your conditional test.
Related
In my php script I'm calling another php scipt via shell_exec().
(The second script only works when executed in the shell, I don't know why but that should not matter here.)
This is the main script calling the other:
$result = shell_exec( 'php /html/wp-content/plugins/neuhof/includes/sync/load_products.php' );
$data = json_decode($result, true);
foreach($data as $product) {
if($product['ID'] !== NULL) {
$wc_product = wc_get_product( $product['ID'] );
?>
<tr id="<?php echo $product['Xhartid']; ?>">
<td class="title column-title has-row-actions column-primary page-title"><strong><?php echo $wc_product->get_name(); ?></strong>
<div class="row-actions">ID: <?php echo $product['ID']; ?> | <span class="view"> Anschauen </span></div>
</td>
<td><?php echo $product['operation']; ?></td>
<td class="state">Warten auf WP-Cron...</td>
</tr>
<?php
} else {
?>
<tr id="<?php echo $product['Xhartid']; ?>">
<td class="title column-title has-row-actions column-primary page-title"><strong><?php echo $product['Xhartbez']; ?></strong>
<div class="row-actions">Noch keine ID vergeben</div>
</td>
<td><?php echo $product['operation']; ?></td>
<td class="state">Warten auf WP-Cron...</td>
</tr>
<?php
}
}
This is the script that should be executed in the shell:
<?php
ini_set('display_errors', 0);
require_once( 'databases.php' ); // Notwendige Datenbankverbindungen
define('SHORTINIT', true); // load minimal WordPress
require_once '/html/wp-load.php'; // WordPress loader
// Mit dieser Funktion werden die Datenbank IDs aus dem ERP nutzbar gemacht
function decodeID($id) {
$unpacked = unpack('Va/v2b/n2c/Nd', $id);
return sprintf('%08X-%04X-%04X-%04X-%04X%08X', $unpacked['a'], $unpacked['b1'], $unpacked['b2'], $unpacked['c1'], $unpacked['c2'], $unpacked['d']);
}
$sql = "
SELECT
Xhartid,
Xbearbdat,
Xhartbez
FROM cms.dbo.xHauptartikel
WHERE Xinternet = '1'
ORDER BY Xhartid ASC
";
$erp_ids = $GLOBALS['erp']->query($sql)->fetchALL();
// Code für Abfrage mit WordPress ID
$sql = "
SELECT distinct
A.post_id as 'ID',
A.meta_value as 'Xhartid',
B.meta_value as 'Xbearbdat'
FROM
wp_postmeta A,
wp_postmeta B
WHERE
A.meta_key = 'Xhartid'
AND
B.meta_key = 'Xbearbdat'
AND
A.post_id = B.post_id
order by Xbearbdat
asc
";
$b = $GLOBALS['cms']->query($sql)->fetchALL();
foreach( $erp_ids as $keya => $a ) {
foreach( $b as $key => $row ) {
if(decodeID($a['Xhartid']) == $row['Xhartid'] ) {
if($a['Xbearbdat'] == $row['Xbearbdat']) {
// Ist akutell
} else {
// Aktualisieren
$list[decodeID($a['Xhartid'])]['operation'] = 'Aktualisieren';
$list[decodeID($a['Xhartid'])]['ID'] = $row['ID'];
$list[decodeID($a['Xhartid'])]['Xhartid'] = $row['Xhartid'];
}
unset($b[$key]);
unset($erp_ids[$keya]);
}
}
}
// Erstellen
foreach($erp_ids as $row) {
$list[decodeID($row['Xhartid'])]['operation'] = 'Erstellen';
$list[decodeID($row['Xhartid'])]['ID'] = NULL;
$list[decodeID($row['Xhartid'])]['Xhartid'] = decodeID($row['Xhartid']);
$list[decodeID($row['Xhartid'])]['Xhartbez'] = $row['Xhartbez'];
}
// Löschen
foreach($b as $row) {
$list[$row['Xhartid']]['operation'] = 'Löschen';
$list[$row['Xhartid']]['ID'] = $row['ID'];
$list[$row['Xhartid']]['Xhartid'] = $row['Xhartid'];
}
if($argv[1] == 'count') {
$ids = 0;
foreach( $list as $product ) {
$ids++;
}
echo $ids;
} elseif ($argv[1] == 'list') {
$ids = NULL;
foreach( $list as $product ) {
$ids[] = $product['Xhartid'];
}
echo json_encode($ids, JSON_UNESCAPED_UNICODE);
} else {
echo json_encode($list, JSON_UNESCAPED_UNICODE);
}
The script returns NULL and even if I try exec() and get the errors, they are also NULL.
I have no idea why this is not working, since shell_exec('ls') and simple "hello world" scripts are working fine!
(The second script only works when executed in the shell, I don't know why but that should not matter here.)
This is probably because MSSQL connection from second script that you have mentioned is blocked in your webserver environment/php configuration used by webserver.
When you are running second script from shell it may work fine as it is working on different environment. But when you are running second script using shell_exec in php script runned by your webserver it is started with the same webserver environment where it was blocked.
So if you are trying to use shell_exec as a "hack" to omit limitations in your webserver environment then it will not work.
You should contact your server administrator to resolve this.
I got a script that shows products according to their id. In the echoed data there is a link which redirects the user to the same page but with a posted id, according to that id I delete a product (id) from my session which is an array. This works perfectly except for the first array value. What could be causing that?
How I have my code at the moment:
//Link to quote page and send product id with it
echo '<p><a class="offertelink" href="offerte.php?product='.$productcr[0]['id'].'">Request a quote</a></p>';
On my quote page:
ob_start();
include 'includes/header.php';
if(!isset($_SESSION['product'])){
$_SESSION['product'] = array();
}
// Check if product is set before putting it in an array
if(isset($_GET['product']) && !in_array($_GET['product'], $_SESSION['product'])){
$_SESSION['product'][] = $_GET['product'];
}
// Implode array to use ids in query
$prods = implode(",", $_SESSION['product']);
Query:
if(count($_SESSION['product']) != 0){
// Get all products with id in session
$offerte = "SELECT * FROM `web_content` WHERE `id` in (".$conn->real_escape_string($prods).") AND state = 1";
$offertecon = $conn->query($offerte);
$offertecr = array();
while ($offertecr[] = $offertecon->fetch_array());
}
And finally below my query:
// Check if id is send, and delete from session accordingly
if(isset($_GET['id'])){
$productID = $_GET['id'];
if( $index = array_search($productID, $_SESSION['product']) ) {
$_SESSION['product'][$index] = null;
unset($_SESSION['product'][$index]);
header('Location: http://www.web.nl/_extern/web/offerte.php');
}
}
if(count($_SESSION['product']) == 0){
echo 'No products added';
}else{
foreach($offertecr as $product){
$offerte_imgs = $product['images']; // Get image parameters of the article
$offertepic = json_decode($offerte_imgs); // Split the parameters apart
if($offertepic->{'image_intro'} != ''){
$image = 'cms/'.$offertepic->{'image_intro'};
}else{
$image = 'http://www.web.nl/_extern/web/cms/images/Producten/Untitled-7.jpg';
}
if($product['id'] != ''){
if (strlen($product['introtext']) > 100){
$shortcat = substr($product['introtext'], 0, 100) . '...';
}else{
$shortcat = $product['introtext'];
}
$deleteLink = "offerte.php?id=".$product['id'];
$offerteoverzicht .= '
<div class="row productofferte">
<a href="producten/'.$product['alias'].'.html">
<div class="col-md-6 offerteimg">
<img style="border:1px solid #ddd;" src="'.$image.'">
</div>
</a>
<div class="desc">
<a style="color:#2d4160;" href="producten/'.$product['alias'].'.html"><p style="font-weight:bold;">'.$product['title'].' </a>
<a href="'.$deleteLink.'">
<i class="fa fa-times" aria-hidden="true"></i>
</a>
</p>
<p>'.$shortcat.'</p>
</div>
</div>';
}
}
}
echo $offerteoverzicht;
I've read a bit on my problem and figured it has something to do with shifting the array if I'm correct. If I print my session all the arrays are deleted from it, except the first one. Doesn't matter what id it is, when I change the first product then that product cannot be deleted from the session.
Your test on finding an element in an array with array_search should compare to false. Doing ! will return false when the element found is at index 0:
Change:
if( $index = array_search($productID, $_SESSION['product']) ) {
to:
if( ($index = array_search($productID, $_SESSION['product'])) !== false ) {
This is puzzling me for the past hours already. Hopefully anybody has the clearing answer and in any case, thank for any support. In need to filter between three values in a data-attribute, which are set in jQuery. These values are changed and put in the attribute, depending on screenwidth. The jQuery part works fine. Now the tricky part is that in the PHP file the right value of the data-width attribute should be used and put as a value for a custom field. This value triggers the right images in a backgroundslider.
I tried the if/else statement in which the attribute value had a variable, which could give the right value for the custumfield, when its value would be equal as the one mad in jQuery. This failed. My knowledge is limited and therefore my files looked like this.
jQuery:
function schermFilter() {
var scherm = $(window).width();
if (scherm >= 320 && scherm <= 480) {
$('#homeslider-wrapper').attr('data-width','slides_mobiel')
console.log(scherm + ": mobiel");
} else if (scherm >= 768 && scherm <= 992) {
$('#homeslider-wrapper').attr('data-width','slides_tablet')
console.log(scherm + ": tablet");
} else {
$('#homeslider-wrapper').attr('data-width','slides_groot')
console.log(scherm + ": groot");
}
};
schermFilter();
$(document).ready(function() {
schermFilter();
});
PHP:
<?php $screenwidth =""; ?>
<?php $args = array(
'post_type' => 'homeslides',
'order' => 'date'
);
$slider = new WP_QUERY($args);
if($slider->have_posts()) : ?>
<div id="homeslider-wrapper" class="slides" data-width="$screenwidth">
<?php while ($slider->have_posts()) : $slider->the_post(); ?>
<?php if ($screenwidth === "slides_mobiel") {
$image = get_field('slides_mobiel');
echo $screenwidth;
} elseif ($screenwidth === "slides_tablet") {
$image = get_field('slides_tablet');
echo $screenwidth;
} else if ($screenwidth === "") {
$image = get_field('slides_groot');
echo $screenwidth;
} else {
$image = get_field('slides_groot');
echo "groot - else";
}
?>
<?php $imageList .= '"'. $image['url'] . '",'; ?>
<?php endwhile; ?>
</div>
<?php endif;wp_reset_postdata(); ?>
<?php $imageList = rtrim($imageList, ','); ?>
I tried isset and property-exists, but without success. Thanks!
I'm pulling comments from MySQL with PDO with a limit of 2 comments to show, yet the query does not retrieve the comments nor can I var_dump neither print_r since it does print anything. I believe it should be an issue with the query itself, thought it hasn't worked for countless hours so I figured I'd ask more experienced programmers.
There is no data outputted onto the page, I'm unsure whether It's an issue with the second_count variable or the post_iD variable.
1. Am I using the while loop correctly?
2. Better to use while loop or foreach?
3. Does var_dump and print_r function not work due to errors in the
query or in PHP.INI from what it seems?
Here is how I'm pulling the data with PDO.
PUBLIC FUNCTION Comments( $post_iD,$second_count ){
if($second_count){
$sth = $this->db->prepare("
SELECT C.com_id, C.uid_fk, C.comment, C.created, U.username, U.photo
FROM comments C, users U
WHERE U.status = '1'
AND C.uid_fk = U.uiD
AND C.msg_id_fk = :postiD
ORDER BY C.com_id ASC LIMIT :second, 2");
$sth->execute(array(':postiD' => $post_iD, ':second' => $second_count));
while( $row = $sth->fetchAll())
$data[] = $row;
if(!empty($data)){
return $data;
}
}
}
And here is how I'm trying to display the information on my page,
<?php
$commentsarray = $Wall->Comments( $post_iD, 0 );
$x=1;
if($x){
$comment_count = count($commentsarray);
$second_count = $comment_count-2;
if($comment_count>2){
?>
<div class="comment_ui" id="view<?php echo $post_iD; ?>">
View all<?php echo $comment_count; ?> comments
</div>
<?php
$commentsarray = $Wall->Comments( $post_iD, $second_count );
}
}
if( $commentsarray ){
foreach($commentsarray as $data){
$com_id = $data['com_id'];
$comment = tolink(htmlcode($data['comment'] ));
$time = $data['created'];
$mtime = date("g:i", $time);
$username = $data['username'];
$com_uid = $data['uid_fk'];
$photo = $data['photo'];
?>
<div class="stcommentbody" id="stcommentbody<?php echo $com_id; ?>">
<div class="stcommentimg">
<img src="<?php echo $photo;?>" class="small_face">
</div>
<div class="stcommenttext">
<?php if($uiD == $com_uid || $uiD == $post_iD ){ ?>
<a class="stcommentdelete" href="#" id="<?php echo $com_id;?>" title="Delete Comment"></a>
<?php } ?>
<div class="stmessage"><?php echo clear($comment); ?></div>
</div>
<div class="stime"><?php echo $mtime;?> — Like</div>
</div>
<?php
}
}
?>
1.Simply like this:
$data = $sth->fetchAll();
if(!empty($data) AND count($data) > 0){
return $data;
}
You don't need while to work with fetchAll.
2.I'ts better to use Exception in every your query then you can see if there is any error.
e.g
try
{
//your PDO stuffs here
}
catch (PDOException $e)
{
echo $e->getMessage();
}
I'm running a while loop which is taking rows from mySQL and echo'ing them out. Pretty standard. However, there needs to be different HTML markup for the first item, next two items, then it continues as normal into the while loop.
Currently, my while loop looking something like this:
while( ( $row = mysql_fetch_object( $result ) ) !== false )
{
// Places ad based of predefined variable frequency
if ($ad == $adFrequency){
echo '<div class="one-advertisement-article clearfix">';
echo '<div class="grid_9 suffix_2"><img src="http://placehold.it/263x75/000000/ffffff"></div>';
echo '<div class="grid_1"><a class="navigate-right-icon ss-icon" href="#">navigateright</a></div>';
echo '</div>';
$ad = 0;
}
echo '<div class="one-news-article clearfix">';
if( $row->imagelibid )
{
$imageid = intval( $row->imagelibid );
$image_result = mysql_query( "SELECT * FROM imagelib WHERE id = {$imageid}", $db );
$image_row = mysql_fetch_object( $image_result );
$image_url = "http://www.#.com/slir/w280/imagelib/" . $image_row->id . "_" . $image_row->filename;
echo '<div class="grid_4"><img src="'.$image_url.'"></div>';
}
else {
echo '<div class="grid_4"><div class="news-placeholder"><span class="ss-icon">ellipsischat</span></div></div>';
}
echo '<div class="grid_7">';
echo '<h2>'.$row->title.'</h2>';
$published_date = date('D, d M Y', strtotime($row->datein));
echo '<p class="published-date">'.$published_date.'</p>';
echo '</div>';
echo '<div class="grid_1">';
echo '<div class="news-item-vertical-sep"> </div>';
echo '<p></p>';
echo '</div>';
echo '</div>';
$ad++;
}
This works fine, but I need to take the first three news items and use this:
echo ' <div class="one-news-featured-article clearfix">';
echo ' <div class="grid_12">';
if( $row->imagelibid )
{
$imageid = intval( $row->imagelibid );
$image_result = mysql_query( "SELECT * FROM imagelib WHERE id = {$imageid}", $db );
$image_row = mysql_fetch_object( $image_result );
$image_url = "http://www.#.com/slir/w280/imagelib/" . $image_row->id . "_" . $image_row->filename;
echo '<div class="large-featured-image-container"><img src="'.$image_url.'">/div>';
}
echo ' <div>';
echo ' <h2>'.$row->title.'</h2>';
echo ' </div>';
echo ' </div>';
echo ' </div>';
echo ' <div class="grid_6">';
Any help? Essentially, it needs to apply the second code to the first three items in the query, then follow through with the code included in the current while loop - like an offset I guess.
Thanks,
R
This is quite simple. I hope I understood your question correctly:
$i = 0;
while ( ( $row = mysql_fetch_object( $result ) ) !== false ) {
if ($i < 3) {
// First code
} else {
// Second code
}
$i += 1;
}
Firstly, you should avoid using any mysql_ functions as they are all deprecated and will be removed from future versions of PHP. I'd recommend using PDO instead. See this tutorial to get started.
Then, it'll simply be a case of doing something like this:
foreach($db->query($query) as $index => $row) {
if ($index < 3) {
// first 3 items
} else {
// other items
}
}
You can do it easaly with an simple counter and an if statement:
$count = 0;
while(fetching) {
if($count < 3) {
// items 1, 2 and 3
} else {
// do normal
}
$count++;
}