Can var_dump a string but can not echo the string - url

I meet a trouble with string.
I use file_get_contents($url) to get content of a website.
$content = tile_get_contents($url);
$arrTmp = explode('>',$content);
var_dump (trim( $arrTmp[100]) ) => result is: string '<td width="33.3333333333%" valign="top"'
echo trim( $arrTmp[100]); => nothing.
Thanks in advance!

your sample seems to be incomplete
you define $arr
where does $arrTmp comes from?
what is $i? is it defined?
whats between the var_dump and echo?
and whats the purpose of this Action?
EDIT:
just tested:
$url = 'http://w3schools.com/tags/ref_standardattributes.asp';
$ctx = stream_context_create(array('http'=> array( 'timeout' => 60 ) ));
$content = file_get_contents($url, false, $ctx);
$arrTmp = explode('>',$content);
for($i = 0; $i < count($arrTmp); $i++)
{
echo '<br />';
echo 'Res->'.htmlspecialchars(trim($arrTmp[$i]));
}
result:
Res-><!DOCTYPE html
Res-><html lang="en-US"
Res-><head
Res-><title
and so on ...
maybe a server-setup-issue? but i have no idea what it could be at your side ...

Related

Wordpress Development: Why am I seeing duplicate urls when I use "wp_get_attachment_image_src( get_post_thumbnail_id()" to get the featured image?

I'm developing a theme on a local development server. But when I try to get the featured image of posts into my home page, I get a 404 error due to the url appearing like this:
http://localhost:8888/crystal/%E2%80%9Dhttp:/localhost:8888/crystal/wp-
content/uploads/2018/04/restaurant-kitchen-inspired-design-ideas.png
See how the domain repeats itself?
I've checked my wp_options table in the database and it looks correct. Both "siteurl" and "home" does have the "http://" already and does not have a "/" after the url. I also already tried adding this script to my wp-config.php:
define('WP_HOME','http://localhost:8888/crystal');
define('WP_SITEURL','http://localhost:8888/crystal');
and this to my functions.php file:
update_option( 'siteurl', 'http://localhost:8888/crystal' );
update_option( 'home', 'http://localhost:8888/crystal' );
but that didn't fix it.
So far, it seems to be only happening when I try to use "wp_get_attachment_image_src( get_post_thumbnail_id()" to get the featured image.
Here is my code:
<?php
$args = array(
'post_type' => 'tournament',
'post_status' => 'publish',
'posts_per_page' => '10'
);
$tournaments_loop = new WP_Query( $args );
if ( $tournaments_loop->have_posts() ) :
while ( $tournaments_loop->have_posts() ) : $tournaments_loop
>the_post();
// Set variables
$title = get_the_title();
$description = get_the_content();
$tournament_date_time = get_field('tournament_date_time');
$location = get_field('location');
$featured_image = wp_get_attachment_image_src(
get_post_thumbnail_id(), 'full' );
$tournament_image = $featured_image[0];
// Output
?>
<div class=”tournament”>
<h3><?php echo $title; ?></h3>
<p><?php echo $tournament_date_time; ?></p>
<p><?php echo $location; ?></p>
<img src=”<?php echo $tournament_image; ?>” alt=”<?php echo
$title; ?>”>
</div>
<?php
endwhile;
wp_reset_postdata();
endif; ?>
Thank you.

Opencart custom page foreach not showing

Am creating a custom page using Opencart. Here am using foreach for displaying customer details.
But customer details not showing.
Following codes:
.tpl
<?php foreach($dealerData as $invoice){ ?>
<label> <b>Dealer Name:</b> <?php echo $invoice['name']; ?></label><br>
<label><b>Dealer TIN Number:</b> <?php echo $invoice['tin_number'];?></label><br>
<?php } ?>
Controller
$query13 = $this->db->query("select concat(firstname, ' ', lastname) as name, tin_number from ".DB_PREFIX."customer where customer_id='".$customer_id."'");
$dataDelar = $query13->rows;
foreach ($dataDelar as $dealer) {
$data['dealerData'][] = array(
'name' => $dealer['name'],
'tin_number' => $dealer['tin_number']
);
}
Why are you putting queries in your controller? You have models van database related functions. An foreach on the variable $invoiceData1 should work, you can see in the print_r that there is 1 array in the array. Did you put the print_r in your controller? So yes, look bellow that, maybe you are overriding $invoiceData1.
EDIT
You are not creating an empty array to put your values in:
$query13 = $this->db->query("select concat(firstname, ' ', lastname) as name, tin_number from ".DB_PREFIX."customer where customer_id='".$customer_id."'");
$dataDelar = $query13->rows;
$data['dealerData'] = [];
foreach ($dataDelar as $dealer) {
$data['dealerData'][] = array(
'name' => $dealer['name'],
'tin_number' => $dealer['tin_number']
);
}

Yii postback value for dropdownlist

Like described in the title, i' trying to use a dropdownlist for post back page.
The post is receive in the controller but the value selected is empty.
Here is my dropdownlist
<?php echo CHtml::dropDownList('groupe',$groupe,
array("A"=>"A","B"=>"B","C"=>"C","D"=>"D","E"=>"E","F"=>"F","G"=>"G","H"=>"H"),
array(
'prompt'=>'--Choisir un groupe--',
'submit'=>CController::createUrl('classement'),
//'data'=>array('groupe'=>'js:this.value'),
)); ?>
And here is my controller
public function actionClassement($groupe="")
{echo $groupe;
if(isset($_POST['groupe'])){echo $_POST['groupe']."ici=".$groupe;
$groupe = $_POST['groupe'];
}echo 'test';
$model = Team::model()->getClassementByGroupe($groupe);
$games = array();
$games = Game::model()->getGameByGroupe($groupe);
//echo '<pre>test';echo print_r($model);echo '</pre>';
$this->render('classement',array(
'model'=>$model,
'groupe'=>$groupe,
'games'=>$games,
));
}
Thanks for your help because i don't find my mistake :-s
You need to wrap your dropdown in a form. This worked. Also note $groupe gets assigned if there is a $_GET variable. Your combo is posting, so you need to use $_POST['groupe']
<form method="post" action="<?= $this->createUrl('') ?>">
<?php
echo CHtml::dropDownList('groupe', $groupe, array(
"A" => "A",
"B" => "B",
"C" => "C",
"D" => "D",
"E" => "E",
"F" => "F",
"G" => "G",
"H" => "H"
), array(
'prompt' => '--Choisir un groupe--',
'submit' => CController::createUrl('classement'),
//'data'=>array('groupe'=>'js:this.value'),
));
?>
</form>
I removed the models for testing the controller.
public function actionClassement($groupe = "") {
echo $groupe;
if (isset($_POST['groupe'])) {
echo $_POST['groupe'] . "ici=" . $groupe;
$groupe = $_POST['groupe'];
}echo 'test';
// $model = Team::model()->getClassementByGroupe($groupe);
// $games = array();
// $games = Game::model()->getGameByGroupe($groupe);
//echo '<pre>test';echo print_r($model);echo '</pre>';
$this->render('view', array(
'groupe' => $_POST['groupe'],
));
}

Front End post form either sets a thumbnail or give me ID, I need both

I have a WP front end post form that I have made for my website, the Idea was to make it so artists can post up their own mixtape submissions!
So far I have managed to get the form to either use the image uploaded as the featured image OR give me the images ID so I can put it in the post content, the problem is I need both! I need the image automatically set as the thumbnail for the post and I need the ID so I can set the form to automatically put the image where I want it in the post!
So far I have located the problem to be this bit of code:
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
// $newupload returns the attachment id
}
}
I say this as, the location of this code is what changes whether I successfully get the ID of the attachment or whether the image is set as the thumbnail.
The whole code is as follows:
function insert_attachment($file_handler,$post_id,$setthumb='false') {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
}
function mixtape_fep($content = null) {
global $post;
// We're outputing a lot of html and the easiest way
// to do it is with output buffering from php.
ob_start(); ?> <?php
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter the wine name';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter some notes';
}
$cover_id = get_post_meta($post->ID, 'thumb', true);
$cover = wp_get_attachment_link($cover_id);
$content = $cover.'<br /><br />'.$description;
$tags = $_POST['post_tags'];
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_category' => array($_POST['cat']), // Usable for custom taxonomies too
'tags_input' => array($tags),
'post_status' => 'publish', // Choose: publish, preview, future, draft, etc.
'post_type' => 'post' //'post',page' or use a custom post type if you want to
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
// $newupload returns the attachment id of the file that
// was just uploaded. Do whatever you want with that now.
}
}
//SET OUR TAGS UP PROPERLY
wp_set_post_tags($pid, $_POST['post_tags']);
//REDIRECT TO THE NEW POST ON SAVE
$link = get_permalink( $pid );
wp_redirect( $link );
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post'); ?>
The issue of assigning the thumbnail and inserting the image was fixed by adding this into the code:
if ( $_FILES ) {
$files = $_FILES['cover'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("cover" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post->ID);
}
}
}
}
and then
set_post_thumbnail( $pid, $newupload );
below the insert post function.

Breadcrumb navigation with Doctrine NestedSet

I have a model that implements NestedSet behaviour:
Page:
actAs:
NestedSet:
hasManyRoots: true
rootColumnName: root_id
columns:
slug: string(255)
name: string(255)
Example fixtures:
Page:
NestedSet: true
Page_1:
slug: slug1
name: name1
Page_2:
slug: slug2
name: name2
children:
Page_3:
slug: page3
name: name3
I am looking for the easiest way to implement breadcrumb navigation (trail). For example, for Page_3 navigation will look like this:
name2 > <a href="page2/page3>name3</a>
Since I hate having any kind of logic in templates (and partials), here's my slightly improved version.
//module/templates/_breadcrumbElement.php
<?php foreach ($node as $child): ?>
<li>
<?php echo $child->getName() ?>
<?php if (count($child->get('__children')) > 0): ?>
<ul>
<?php include_partial('node', array('node' => $child->get('__children'), 'parent' => $child)) ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
So, all the logic for building a url is now in Page::getPath() method.
class Page extends BasePage
{
/**
* Full path to node from root
*
*/
protected $path = false;
public function __toString()
{
return $this->getSlug();
}
public function getPath($parent = null)
{
if (!$this->path)
{
$this->path = join('/', null !== $parent ? array($parent->getPath(), $this) : array($this));
}
return $this->path;
}
}
What I don't like it having to pass $parent to Page::getPath(). It just doesn't make any semantical sense.
Almost the same as in the other question, but you have to add a 'parentUrl' variable :
//module/templates/_breadcrumbElement.php
foreach ($node->get('__children') as $child) :
if ($child->isAncestorOf($pageNode)):
$currentNodeUrl = $parentUrl . $child->getSlug() . '/';
echo link_to($child->getName(), $currentNodeUrl) . ' > ' ;
include_partial('module/breadcrumbElement', array('node' => $child, 'pageNode' => $pageNode, 'parentUrl' => $currentNodeUrl));
endif;
endforeach;
Feed it the root of your tree as $node (hydrate it hierarchically), the node of the current page as $pageNode, and '' as $currentNodeUrl and add ' > ' and the link to the current page.
Why does this solution use recursion and not getAncestors()? Because your urls seem to imply recursion.
Another answer, more simple (and perhaps more efficient), with getAncestors() and recursion:
//module/templates/_breadcrumbElement.php
if ($node = array_pop($nodes)) // stop condition
{
$currentNodeUrl = $parentUrl . $node->getSlug() . '/';
echo link_to($node->getName(), $currentNodeUrl) . ' > ' ;
include_partial('module/breadcrumbElement', array(
'nodes' => $nodes, 'parentUrl' => $currentNodeUrl));
}
Call this with an array of ancestor nodes or find a way to pop a Doctrine_Collection if you want to use it with getAncestors() directly.
Again, all your problem comes from the fact that your urls are recursively computed, it would be simpler and faster to display if you had a colum path with the current url (but then you would have to compute, update it), etc... consider doing this if you have more reads than writes (if your tree does not change often).

Resources