I am working on project which shows articles and this was done by article manager (a ready to use php script) but I have a problem, I want to show only four article titles and summaries from old list of article randomly which contains 10 article. Any idea how to achieve this process?
I have auto generated summary of article
<div class="a1">
<h3><a href={article_url}>{subject}</h3>
<p>{summary}<p></a>
</div>
When a new article is added the above code will generated and add into summary page. I want to add it to side of the main article page, where user can see only four article out of ten or more randomly.
<?php
$lines = file_get_contents('article_summary.html');
$input = array($lines);
$rand_keys = array_rand($input, 4);
echo $input[$rand_keys[0]] . "<br/>";
echo $input[$rand_keys[1]] . "<br/>";
echo $input[$rand_keys[2]] . "<br/>";
echo $input[$rand_keys[3]] . "<br/>";
?>
Thanks for your kindness.
Assuming I understood you correctly - a simple solution.
<?php
// Settings.
$articleSummariesLines = file('article_summary.html', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$showSummaries = 4;
// Check if given file is valid.
$validFile = ((count($articleSummariesLines) % 4 == 0) ? true : false);
if(!$validFile) {
die('Invalid file...');
}
// Count articles and check wether all those articles exist.
$countArticleSummaries = count($articleSummariesLines) / 4;
if($showSummaries > $countArticleSummaries) {
die('Can not display '. $showSummaries .' summaries. Only '. $countArticleSummaries .' available.');
}
// Generate random article indices.
$articleIndices = array();
while(true) {
if(count($articleIndices) < $showSummaries) {
$random = mt_rand(0, $countArticleSummaries - 1);
if(!in_array($random, $articleIndices)) {
$articleIndices[] = $random;
}
} else {
break;
}
}
// Display items.
for($i = 0; $i < $showSummaries; ++$i) {
$currentArticleId = $articleIndices[$i];
$content = '';
for($j = 0; $j < 4; ++$j) {
$content .= $articleSummariesLines[$currentArticleId * 4 + $j];
}
echo($content);
}
Related
I have this script for my site and I think I have a problem in the for line. If I update my user_xp_amount with 256 the script works fine. However, when I update with a higher XP, such as 785, the script is showing me that I have level 1 instead of level 2 with 256xp. Why is this not behaving the way I desire?
PHP Script:
<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'db');
$user_id = 4;
function get_user_xp($user_id, $mysqli) {
$sql = $mysqli->query("SELECT user_xp_amount FROM users_xp WHERE user_id='$user_id'");
$row = $sql->fetch_assoc();
return $row['user_xp_amount'];
}
$xp = array(0,256,785,1656,2654);
$my_xp = get_user_xp($user_id, $mysqli); // where 4 is my user id
for($i = 0; $i < count($xp); $i++) {
if($my_xp == $xp[$i]) {
echo 'I\'m on level ', ($i+1);
break;
}
else {
if(isset($xp[$i+1])) {
if($my_xp > $xp[$i] && $my_xp <= $xp[$i + 1]) {
echo 'My next level is ', ($i+2), ' and I need ', $xp[$i+1], ' more points for achieving it!';
break;
} else {
echo 'My next level is ', ($i+1), ' and I need ', $xp[$i], ' more points for achieving it!';
break;
}
}
}
}
?>
MySQL DB:
user_xp_id Primar bigint(20) | user_id bigint(20) | user_xp_amount bigint(20)
The reason it's not working can be found with some basic debugging: think through your for loop step by step. If that fails, try something like
// snip
for($i = 0; $i < count($xp); $i++) {
print $i; // show me which step I'm on
if($my_xp == $xp[$i]) {
// snip
Try that, and the reason will pop out at you.
This is an essential part of debugging: be careful what you assume. Your assumption is usually your mistake.
Only after you go through that exercise should you look any further into this answer. (You may not even want to!)
A hint that'll make it easier to code: you can keep reassigning values until you exit the loop.
$xp = array(0,256,785,1656,2654);
$my_xp = 254;
for($i = 0; $i < count($xp); $i++) {
// if we've looped beyond our xp, exit with last assigned (current) values
if($my_xp < $xp[$i]) { break; }
$my_level = $i;
// if index + 1 is in range, assign values
if($i+1<count($xp)) {
$next_level = $i+1;
$xp_needed = $xp[$i+1] - $my_xp;
// if index + 1 is out of range, give default values
} else {
$next_level = $i;
$xp_needed = 0;
}
}
printf("<div>Current Level: %d (%d xp)</div>" .
"<div>Next Level: %d (%d xp)</div>" .
"<div>XP Needed: %d</div>",
$my_level +1, // correct off-by-1 error of index
$my_xp,
$next_level +1, // correct off-by-1 error of index
$xp[$next_level],
$xp_needed
);
I have an interface where a user will enter the name of a company. It then compares what they typed to current entries in the database, and if something similar is found it presents them with options (in case they misspelled) or they can click a button which confirms what they typed is definitely new and unique.
The problem I am having is that it is not very accurate and often brings up dozens of "similar" matches that aren't that similar at all!
Here is what I have now, the first large function I didn't make and I am not clear on what exactly it does. Is there are much simpler way to acheive what I want?
// Compares strings and determines how similar they are based on a nth letter split comparison.
function cmp_by_optionNumber($b, $a) {
if ($a["score"] == $b["score"]) return 0;
if ($a["score"] > $b["score"]) return 1;
return -1;
}
function string_compare($str_a, $str_b)
{
$length = strlen($str_a);
$length_b = strlen($str_b);
$i = 0;
$segmentcount = 0;
$segmentsinfo = array();
$segment = '';
while ($i < $length)
{
$char = substr($str_a, $i, 1);
if (strpos($str_b, $char) !== FALSE)
{
$segment = $segment.$char;
if (strpos($str_b, $segment) !== FALSE)
{
$segmentpos_a = $i - strlen($segment) + 1;
$segmentpos_b = strpos($str_b, $segment);
$positiondiff = abs($segmentpos_a - $segmentpos_b);
$posfactor = ($length - $positiondiff) / $length_b; // <-- ?
$lengthfactor = strlen($segment)/$length;
$segmentsinfo[$segmentcount] = array( 'segment' => $segment, 'score' => ($posfactor * $lengthfactor));
}
else
{
$segment = '';
$i--;
$segmentcount++;
}
}
else
{
$segment = '';
$segmentcount++;
}
$i++;
}
// PHP 5.3 lambda in array_map
$totalscore = array_sum(array_map(function($v) { return $v['score']; }, $segmentsinfo));
return $totalscore;
}
$q = $_POST['stringA'] ;
$qLengthMin = strlen($q) - 5 ; // Part of search calibration. Smaller number = stricter.
$qLengthMax = strlen($q) + 2 ; // not in use.
$main = array() ;
include("pdoconnect.php") ;
$result = $dbh->query("SELECT id, name FROM entity_details WHERE
name LIKE '{$q[0]}%'
AND CHAR_LENGTH(name) >= '$qLengthMin'
#LIMIT 50") ; // The first letter MUST be correct. This assumption makes checker faster and reduces irrelivant results.
$x = 0 ;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$percent = string_compare(strtolower($q), strtolower(rawurldecode($row['name']))) ;
if($percent == 1) {
//echo 1 ;// 1 signifies an exact match on a company already in our DB.
echo $row['id'] ;
exit() ;
}
elseif($percent >= 0.6) { // Part of search calibration. Higher deci number = stricter.
$x++ ;
$main[$x]['name'] = rawurldecode($row['name']) ;
$main[$x]['score'] = round($percent, 2) * 100;
//array_push($overs, urldecode($row['name']) . " ($percent)<br />") ;
}
}
usort($main, "cmp_by_optionNumber") ;
$z = 0 ;
echo '<div style="overflow-y:scroll;height:175px;width:460px;">' ;
foreach($main as $c) {
if($c['score'] > 100) $c['score'] = 100 ;
if(count($main) > 1) {
echo '<div id="anysuggested' . $z . '" class="hoverdiv" onclick="selectAuto(' . "'score$z'" . ');">' ;
}
else echo '<div id="anysuggested' . $z . '" class="hoverdiv" style="color:#009444;" onclick="selectAuto(' . "'score$z'" . ');">' ;
echo '<span id="autoscore' . $z . '">' . $c['name'] . '</span></div>' ;
$z++ ;
}
echo '</div>' ;
Comparing strings is a huge topic and there are many ways to do it. One very common algorithm is called the Levenshtein difference. This is a native implementation in PHP but none in MySQL. There is however an implementation here that you could use.
You need aproximate/fuzzy string matching.
Read more about
http://php.net/manual/en/function.levenshtein.php, http://www.slideshare.net/kyleburton/fuzzy-string-matching
The best way would be to use some index based search engine like SOLR http://lucene.apache.org/solr/.
I'm working on a printing a baseball team lineup, via php. I want to print a place holder for a missing Player 6 (or any missing position)
So if Player 1 -> Player 5 is OK print, NO Player #6 print place holder, Player 7 -> Player 9 is OK print. I tried to simplify the code. I have tried solving this every which way but I keep getting stuck.
CODE:
$rot = array();
$pos = array();
$jn = array();
$x = 1;
// loads up the arrays from the db
while ( $rot[$x], $pos[$x], $jn[$x])= $r->fetch_row() ) {
$x++;
}
// counts the actual number of players in linuep
// used for validation and error display
$num_players = mysqli_num_rows($r);
// controls the lineup position
for ($i = 1; $i <= 15; $i++){
if($rot[$i] == $i) {
//prints player
$lineup .= "<div data-fp='" . $pos[$i] . "'>" .$jn[$i]. "</div>";
} else {
// prints place holder
$text = "This Position needs to be filled before the next game.";
$lineup .= "<div id='pid' data-rel='".$text."' data-fp='' data-pid='' data-jn='' title=''>x</div>";
}
}
I also tried this to iterate through the array rot[] to find the matching position and print the line but it actually prints the holder repeatedly.
// controls the lineup position
for ($x = 1; $x <= 15; $x++){
for ($i = 1; $i <= ($num_players+1); $i++) {
if ($x == $i) {
//prints player
$lineup .= "<div data-fp='" . $pos[$i] . "'>" .$jn[$i]. "</div>";
} else {
// prints place holder
$text = "This Position needs to be filled before the next game.";
$lineup .= "<div id='pid' data-rel='".$text."' data-fp='' data-pid='' data-jn='' title=''>x</div>";
}
}
}
What about:
# index all players by position while taking them from the database
$players = array();
while ( $row = $r->fetch_row() ) {
list($rot, $pos, $jn) = $row;
$players[$pos] = compact(array('rot', $pos, $jn);
}
...
# line-up players
for ($pos = 1; $pos <= 15; $pos++)
{
$playerExists = isset($players[$pos]);
if ($playerExists)
{
# do this ...
}
else
{
# do that ...
}
}
I think you are creating an array where all numerical elements are filled (i.e. you'll always have a 1 thru 15) and your mistake is in the
if($rot[$i] == $i) {
When populating the arrays from the database, add this line:
$playertoid = array_flip($pos); # pos is the player number array?
i.e.
while ( ($rot[$x], $pos[$x], $jn[$x])= $r->fetch_row() ) {
$x++;
}
$playertoid = array_flip($pos);
Now you've created a reverse lookup table where the index is the player number.
Replace the
if($rot[$i] == $i) {
line with:
if (isset($playertoid[$i])) {
<?php
$rows_per_page = 2;
$cols_per_page = 2;
$image_href = '<a href=/';
$image_links = array('file1/page1>', 'file2/page2>', 'file3/page3>',
'file4/page4>', 'file5/page5>', 'file6/page6>', 'file6/page6>',
'file/page7>', 'file/page8>', 'subfile/page9>');
$img_srcs = '<img src="https://s3.amazonaws.com/imagetitle/';
$images = array();
for($i = 1; $i < 10; $i++)
{
$images[$i] = $i;
}
$image_ending = '.png" height="200" width="200" /></a>';
$image_descriptions = array('<br />something', '<br />description',
'<br />arbitrary', 'random', '<br />you', '<br />get', '<br />the',
'<br />idea', '<br />itsdescriptions');
$total_images = count($images);
$images_per_page = $rows_per_page * $cols_per_page;
$total_images = count($images);
$total_pages = ceil($total_images / $images_per_page);
$current_page = (int) $_GET['page'];
if($current_page<1 || $current_page>$total_pages)
{
$current_page = 1;
}
//Get records for the current page
$page_image_links = array_splice($image_links, ($current_page-1)*$images_per_page, $images_per_page);
$page_images = array_splice($images, ($current_page-1)*$images_per_page, $images_per_page);
$page_image_descriptions = array_splice($image_descriptions, ($current_page-1)*$images_per_page, $images_per_page);
$slots = "<table border=\"1\">";
for($row=0; $row<$rows_per_page; $row++)
{
$slots .= "<tr>";
for($col=0; $col<$cols_per_page; $col++)
{
$imgIdx = ($row * $rows_per_page) + $col;
$img = (isset($page_images[$imgIdx])) ? "{$image_href}{$page_image_links[$imgIdx]}{$img_srcs}{$page_images[$imgIdx]}{$image_ending}{$page_image_descriptions[$imgIdx]}" : ' ';
$slots .= "<td>$img</td>";
}
$slots .= "</tr>";
}
$slots .= "</table>";
//Create pagination links
$first = "First";
$prev = "Prev";
$next = "Next";
$last = "Last";
if($current_page>1)
{
$prevPage = $current_page - 1;
$first = "First";
$prev = "Prev";
}
if($current_page<$total_pages)
{
$nextPage = $current_page + 1;
$next = "Next";
$last = "Last";
}
?>
<html>
<title></title>
<body>
<h2>Here are the records for page <?php echo $current_page; ?></h2>
<ul>
<?php echo $slots; ?>
</ul>
Page <?php echo $current_page; ?> of <?php echo $total_pages; ?>
<br />
<?php echo "{$first} | {$prev} | {$next} | {$last}"; ?>
</body>
</html>
So basically this code above makes it easy to put up images and their links. For now, I don't have that many images, so the page runs super fast. However, I'm thinking in the long run. If I have say, 10,000 images, it's gonna take a long time to process each paginated page, as I would have $image_links of file1/page1 up to file10000/page10000, and 10000 descriptions! Is there a way to stop the web browser from reading, or skip, certain parts of the script (the $image_links and $image_descriptions in my case)? That way, it won't need to read through all 10000 $image_links and $image_descriptions.
Thanks!
I'm going to suggest another approach. Using arrays for this is not ideal; it really feels like it should be a database driven app.
If you decide to go for that approach, pagination becomes much easier. You can just ask your database engine for the corresponding records. For example, in MySQL, you'd ask for the data something like this:
select * from tableA order by id limit 10 offset 20;
This would return id's 21 thru 30.
... and, would scale much better. Of course, if you've never done any work with databases, there is quite a learning curve.
If you want to end execution at a certain place, just call exit;
Try using the onload function on your < body >'s html, so it will load the page at once
Working on implementing image descriptions to a php run gallery and can't seem to figure out how to call each text file for each individual image. I would need to place a div in the code for the image and then call the include.
//total number of images
$total = 77;
//max number of thumbnails per page
$max = 9;
//what image do we want to start from?
$startcount = $_GET["start"];
//if there is not a defined starting image, we start with the first
if(empty($startcount))
{
$startcount = 0;
}
//start off the loop at 1
$loop = 1;
//start the loop
while($loop <= $max)
{
//for the picture labels
$num = $startcount + $loop;
if($num > $total)
{
$num = $num - 1;
break;
}
// Add class="last" to every third list item
if(is_int($num / 3))
{
$last = ' class="last"';
}
else
{
$last = "";
}
//the code for the image
echo '
<li'.$last.'><img src="images/portfolio/thumbs/pic-'.$num.'-thumb.jpg" width="256" height="138" alt="Thumbnail of image '.$num.'" /><div>'.$num.'</div></li>';
I see that I can call the text files by number using '.$num.' (I have 77 individual text files with a phrase in each) but how do I tell it to call the files?
Assuming the description files are named something like "description_$num.txt", you can simply use readfile.
echo "<li${last}><a href='images/portfolio/pic-${num}.jpg' rel='milkbox[gall1]'>
<img src='images/portfolio/thumbs/pic-${num}-thumb.jpg' width='256'
height='138' alt='Thumbnail of image ${num}'/>
</a><div>";
readfile("description_${num}.txt");
echo '</div></li>';
Note that you don't "call" files in PHP, you either include them (which interprets them as scripts) or read them (which dumps them to the output).
For each file you need to do something like that:
<?php
$f=fopen($file,'r');
$data='';
while(!feof($f))
$data.=fread($f,$size);
fclose($f);
// do whatever you want with the file content.
?>
I made use of file_get_contents, and split the output a bit so you can more easily modify it:
<?php
$total = 77;
$max = 9;
$startcount = $_GET["start"];
$loop = 1;
if(empty($startcount)) $startcount = 0;
while($loop <= $max)
{
$num = $startcount + $loop;
if($num > $total)
{
$num--;
break;
}
$last = ($num % 3 == 0 ? ' class="last"' : '');
$output = "<li $last>";
$output .= "<a href=\"images/portfolio/pic-$num.jpg\" rel=\"milkbox[gall1]\">";
$output .= "<img src=\"images/portfolio/thumbs/pic-$num-thumb.jpg\" width=\"256\" height=\"138\" alt=\"Thumbnail of image $num\">";
$output .= "</a>";
$output .= "<div>";
$output .= file_get_contents("textfile-" . $num . ".txt"); // assuming this is where you want the phrase
$output .= "</div>";
$output .= "</li>";
echo $output;
}
?>