I'm uning a php script to get a random news article from a directory and to display it on my website, this works fine, however when I try to recreate it to show something else at random, the new one doesn't work - Here is my code:
<?php
function random_unit($unit_dir = 'sections/units')
{
$units = glob($unit_dir . '/*.php');
$unit = array_rand($units);
return $units[$unit];
}
$unit_1 = random_unit("sections/units");
?>
<?php $unit_1 = file_get_contents( $unit_1 ); ?>
<?php echo $unit_1; ?> // Echo's The Contents Of Selected Random File
Can anyone see where I might have gone wrong?
Related
Im new to Php. I kinda need code to open Links in browser when script is loaded.
heres my code below.
<?php
$links = array_open("https://stackoverflow.com/",
"https://outlook.office.com/",
"https://www.protectedtext.com/",
"https://www.adobe.com/",
"https://www.linkedin.com");
echo $links[array_rand($links)];
?>
Hi change array_open to array
<?php
$links = array("https://stackoverflow.com/", "https://outlook.office.com/", "https://www.protectedtext.com/", "https://www.adobe.com/", "https://www.linkedin.com");
echo $links[array_rand($links)];
?>
And if you want to send the browser to the link you would send a location header like so...
<?php
$links = array("https://stackoverflow.com/", "https://outlook.office.com/", "https://www.protectedtext.com/", "https://www.adobe.com/", "https://www.linkedin.com");
header('Location: ' . $links[array_rand($links)]);
exit;
?>
So I have this non wordpress database and I need to put all the posts in the Wordpress database, to do that I use this script but only uploads 1037 rows from 11231.
This is the script.
<?php
ini_set('max_execution_time', 300);
require("connect.php");
$conn = mysqli_connect("127.0.0.1","root","psswd");
mysqli_select_db($conn,"db_test");
$results = mysqli_query($conn,"SELECT * FROM test");
$i = 0;
while ($row = mysqli_fetch_array($results,MYSQL_ASSOC)) {
$post = array();
$post['post_status'] = 'publish';
$post['ID']=$row['idPost'];
$post['post_date']=$row['date'];
$post['post_date_gmt']=$row['date'];
$post['post_content']=$row['body'];
$post['post_title']=$row['title'];
$posts[$i] = $post;
$i++;
}
mysqli_free_result($results);
mysqli_close($conn);
require('C:\xampp\htdocs\test\wordpress\wp-load.php');
foreach ($posts as $post) {
wp_insert_post($post);
}
?>
It doesent show any error.
I tried to execute the script from the CMD, from Firefox and Chrome.
This is what I tried so far:
· Add ini_set('max_execution_time', 300);.
· Add set_time_limit(0); where the wp_insert_post() function is.
· Change max_allowed_packet size in my.ini.
If we can't make this work I thought that I could do an INSERT TO, but with this the URLs won't be generated.
Thank you.
Try setting the second param to true wp_insert_post($post,true) . You can see any errors . Encountered .
I'm trying to build my app with a MVC pattern, to do that I followed a book. In my index.php page I have a button, when I click on that button I want to call my formController to display a form, I'm trying to do that first just showing a simple text with <h1> but it doesnt show any text just a blank page.
Index.php
<?php
//check for errors
error_reporting(E_ALL);
ini_set("display errors",1);
include_once "Models/PageData.php";
$pageData = new PageData();
$pageData->title = "OSeuCondómiono - Gere tudo apartir de uma única plataforma";
$pageData->bootstrap = "<link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'rel='stylesheet'>";
$pageData->jquery = "<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js'></script>";
$pageData->bootstrapScript = "<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>";
$pageData->googleApiFont = "<link href='https://fonts.googleapis.com/css?family=Merriweather|Open+Sans|Oswald|Roboto'>";
$pageData->addCss("Css/style.css");
$pageData->content = include_once "Views/home.php";
$formButton = isset($_GET['form-nav']);
if($formButton){
$pageData->content = include_once "Controllers/loginFormCont.php";
}
$page = include_once "Views/layout.php";
echo $page;
?>
loginFormControler
<?php
return "<h1>HIIIII</h1>";
?>
that <h1> doesn't display. I don't know why.
Did you try echo instead of return. Return is better to be used inside a function. Yours is used outside a function.
I have a simple function, which I'm trying to turn turn into a query. The idea is to grab the number of facebook likes (which I've done), and pass it into a simple equation (which is ready) to work out the percentage of likes against a target number. I know that the php controling the percentage will return 0.0 at the moment - that's fine for now :) I just can't work out how to get from the function to a variable...
Code as follows:
<?php
function bfan() {
$pageID = 'VisitTywyn';
$info = json_decode(file_get_contents('http://graph.facebook.com/' . $pageID));
echo $info->likes;
} ?>
<?php bfan(); ?>
<?php
echo number_format(($num_amount/$num_total)*100, 1);
?>
Thanks for your help!
What you want to do is return your result so it can be passed to a variable.
<?php
function bfan() {
$pageID = 'VisitTywyn';
$info = json_decode(file_get_contents('http://graph.facebook.com/' . $pageID));
return $info->likes;
} ?>
<?php $something = bfan(); ?>
I am working on a user interface, "dashboard" of sorts which has some div boxes on it, which contain information relevant to the current logged in user. Their calendar, a todo list, and some statistics dynamically pulled from a google spreadsheet.
I found here:
http://code.google.com/apis/spreadsheets/data/3.0/reference.html#CellFeed
that specific cells can be requested from the sheet with a url like this:
spreadsheets.google.com/feeds/cells/0AnhvV5acDaAvdDRvVmk1bi02WmJBeUtBak5xMmFTNEE/1/public/basic/R3C2
I briefly looked into Zend GData, but it seemed way more complex that what I was trying to do.
So instead I wrote two php functions: (in hours.php)
1.) does a file_get_contents() of the generated url, based on the parameters row, column, and sheet
2.) uses the first in a loop to find which column number is associated with the given name.
So basically I do an ajax request using jQuery that looks like this:
// begin js function
function ajaxStats(fullname)
{
$.ajax({
url: "lib/dashboard.stats.php?name="+fullname,
cache: false,
success: function(html){
document.getElementById("stats").innerHTML = html;
}
});
}
// end js function
// begin file hours.php
<?php
function getCol($name)
{
$r=1;
$c=2;
while(getCell($r,$c,1) != $name)
{ $c++; }
return $c;
}
function getCell($r, $c, $sheet)
{
$baseurl = "http://spreadsheets.google.com/feeds/cells/";
$spreadsheet = "0AnhvV5acDaAvdDRvVmk1bi02WmJBeUtBak5xMmFTNEE/";
$sheetID = $sheet . "/";
$vis = "public/";
$proj = "basic/";
$cell = "R".$r."C".$c;
$url = $baseurl . $spreadsheet . $sheetID . $vis . $proj . $cell . "";
$xml = file_get_contents($url);
//Sometimes the data is not xml formatted,
//so lets try to remove the url
$urlLen = strlen($url);
$xmlWOurl = substr($xml, $urlLen);
//then find the Z (in the datestamp, assuming its always there)
$posZ = strrpos($xmlWOurl, "Z");
//then substr from z2end
$data = substr($xmlWOurl, $posZ + 1);
//if the result has more than ten characters then something went wrong
//And most likely it is xml formatted
if(strlen($data) > 10)
{
//Asuming we have xml
$datapos = strrpos($xml,"<content type='text'>");
$datapos += 21;
$datawj = substr($xml, $datapos);
$endcont = strpos($datawj,"</content>");
return substr($datawj, 0,$endcont);
}
else
return $data;
}
?>
//End hours.php
//Begin dashboard.stats.php
<?php
session_start();
// This file is requested using ajax from the main dashboard because it takes so long to load,
// as to not slow down the usage of the rest of the page.
if (!empty($_GET['name']))
{
include "hours.php";
// GetCollumn of which C#R1 = users name
$col = getCol($_GET['name']);
// then get cell from each of the sheets for that user,
// assuming they are in the same column of each sheet
$s1 = getcell(3, $col, 1);
$s2 = getcell(3, $col, 2);
$s3 = getcell(3, $col, 3);
$s4 = getcell(3, $col, 4);
// Store my loot in the session varibles,
// so next time I want this, I don't need to fetch it
$_SESSION['fhrs'] = $s1;
$_SESSION['fdol'] = $s2;
$_SESSION['chrs'] = $s3;
$_SESSION['bhrs'] = $s4;
}
//print_r($_SESSION);
?>
<!-- and finally output the information formated for the widget-->
<strong>You have:</strong><br/>
<ul style="padding-left: 10px;">
<li> <strong><?php echo $_SESSION['fhrs']; ?></strong> fundraising hours<br/></li>
<li>earned $<strong><?php echo $_SESSION['fdol']; ?></strong> fundraising<br/></li>
<li> <strong><?php echo $_SESSION['chrs']; ?></strong> community service hours<br/></li>
<li> <strong><?php echo $_SESSION['bhrs']; ?></strong> build hours <br/></li>
</ul>
//end dashboard.stats.php
I think that where I am loosing my 4 secs is the while loop in getCol() [hours.php]
How can I improve this, and reduce my loading time?
Should I just scrap this, and go to Zend GData?
If it is that while loop, should i try to store each users column number from the spreadsheet in the user database that also authenticates login?
I didn't have the proper break in the while loop, it continued looping even after it found the right person.
Plus the request take time to go to the google spreadsheet. About .025 second per request.
I also spoke with a user of ZendGdata and they said that the request weren't much faster.