yii2 multiple select with image - php

I used Yii2 multiple selection dropdown with image , it is working fine at create but not showing me selected values on update...
Form:
<?php
$allProducts = Product::find()->where('active = 1')->all();
$prArr = array();
if ($allProducts) {
foreach ($allProducts as $allProduct) {
echo '<option value="' . $allProduct->id . '" style="color: #000; height: 50px; padding-left: 70px;padding-top: 15px;background-image: url(\'' . $allProduct->getThumb() . '\');background-repeat: no-repeat;background-size: 65px auto;">' . $allProduct->title . '</option>';
}
}
?>
Controller:
$oldRels = ProductRelated::find()->where('main_product_id = :main_product_id', ['main_product_id' => $model->id])->all();
if ($oldRels) {
foreach ($oldRels as $oldRel) {
$oldRel->delete();
}
}
if (isset($_POST['relProducts']) and ! empty($_POST['relProducts'])) {
foreach ($_POST['relProducts'] as $relProduct_id) {
$relProduct = new ProductRelated;
$relProduct->main_product_id = $model->id;
$relProduct->rel_product_id = $relProduct_id;
$relProduct->save(false);
}
}
How I can show multi selected values in dropdown with images when I update my recored?

you can try this:
<select id="relProductSelect" name="relProducts[]" multiple>
<?php
$allProducts = Product::find()->where('active = 1')->all();
$arrRelatedProducts = ArrayHelper::map(ProductRelated::find()->where('main_product_id = :main_product_id', ['main_product_id' => $model->id])->all(), 'rel_product_id', 'rel_product_id');
if($allProducts){
foreach($allProducts as $allProduct){
if(in_array($allProduct->id, $arrRelatedProducts)){
echo '<option value="'.$allProduct->id.'" selected style="color: #000; height: 50px; padding-left: 70px;padding-top: 15px;background-image: url(\''.$allProduct->getThumb().'\');background-repeat: no-repeat;background-size: 65px auto;">'.$allProduct->title.'</option>';
}else{
echo '<option value="'.$allProduct->id.'" style="color: #000; height: 50px; padding-left: 70px;padding-top: 15px;background-image: url(\''.$allProduct->getThumb().'\');background-repeat: no-repeat;background-size: 65px auto;">'.$allProduct->title.'</option>';
}
}
}
?>
</select>

Related

How To Retrieve Column's Data of a Table, Store It Into An Array, and Finally Echo The Array

Here's what I'm basically trying to do:
Retrieve the values of a column
Store those values into an array in PHP
Echo each value with a line break in between each value
Here's my attempted code:
<?php
$connection = mysqli_connect("localhost", "root", "bruhfrogzombie098", "growtapi_social");
if (!$connection) {
die("Failed to connect to MYSQL: " . mysqli_connect_errno());
};
$members = mysqli_query($connection, "SELECT Username FROM s_users");
$members_status = mysqli_query($connection, "SELECT Status_Content FROM s_users");
$members_array = array();
while ($member = mysqli_fetch_assoc($members)) {
$members_array[] = $member;
};
$members_status_array = array();
while ($status = mysqli_fetch_assoc($members_status)) {
$members_status_array[] = $status;
};
?>
And this is where I want to echo out the values:
<center>
<h1>Members Directory</h1>
<div style="width: 20%; height; 75%; border: 3px solid black; margin: auto; overflow: hidden; overflow-y: scroll;">
<?php echo $members_array['$member'];
echo "<br />";
?>
</div>
</center>
I don't receive any errors, but the problem is that nothing shows up in the div, meaning that I either didn't retrieve the data properly or didn't use it right.
Note: I've finally moved on to writing up-to-date code, so I hope no one here comments that somewhere in this code I have outdated code ( ͡° ͜ʖ ͡°)
$members_array = array();
while ($member = mysqli_fetch_assoc($members)) {
$members_array[] = $member;
//$members_array is array and $member is array so $members_array like $members_array[][];
};
<center>
<h1>Members Directory</h1>
<div style="width: 20%; height; 75%; border: 3px solid black; margin: auto; overflow: hidden; overflow-y: scroll;">
<?
$member_count = count($members_array);
for( $i = 0 ; $i < $member_count ; $i++ ){
echo $member_array[$i]['Username'];
echo "<br />";
}
?>
</div>
</center>

PHP - Run the same php function multiple times on the same file

I'm currently creating a database / table list sort of system, where it will display the current likes/dislikes of a user. The likes/dislikes of a user is fetched, and managed by a function from a seperate file, called displayRating(). I use require_once to call the seperate file that it is located in, at the beginning of my list, then get down to business.
I then go ahead and have my
foreach(...){
...
displayRating($id);
...
}
However I'm getting an error, which seems to be telling me that I can't call a function multiple times within the same page. Here is my exact (unoptimized) code:
vote.php
<?php
function displayRating($id){
if(isset($id)){
require_once('medoo.min.php');
// Start up the D.B. Connection
$database = new medoo([
// required
'database_type' => 'mysql',
'database_name' => '****',
'server' => 'localhost',
'username' => '****',
'password' => '****',
'charset' => 'utf8',
// driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php
'option' => [
PDO::ATTR_CASE => PDO::CASE_NATURAL
]]);
// xf_user_field_value DB. Amount of ratings: 506c617965725f7261746573 Current Score: 706c617965725f726174696e67
$accountinfo = $database->select('xf_user_field_value', ['field_value'], ["user_id"=>$id]);
$vScore = $accountinfo[5]['field_value'];
// Processing Time
$phrase = '<div class="bc"><div data-hint="%1$s" class="hint--right vote %2$s"></div></div>';
if($vScore == 0){
printf($phrase, 'Neutral Score', 'v_equal');
return;
} elseif ($vScore > 0){
// Positive Here
switch($vScore){
case 1:
printf($phrase, '1 Like', 'v_p1 hint--success');
break;
case 2:
printf($phrase, '2 Likes', 'v_p2 hint--success');
break;
case 3:
printf($phrase, '3 Likes', 'v_p3 hint--success');
break;
case 4:
printf($phrase, '4 Likes', 'v_p4 hint--success');
break;
case 5:
printf($phrase, '5 Likes', 'v_p5 hint--success');
break;
case 6:
printf($phrase, '6 Likes', 'v_p6 hint--success');
break;
case 7:
printf($phrase, '7 Likes', 'v_p7 hint--success');
break;
default:
printf($phrase, $vScore . ' Likes', 'v_p7 hint-success');
break;
}
} elseif ($vScore < 0){
// Negative Here
switch($vScore){
case -1:
printf($phrase, '1 Dislike', 'v_m1 hint--error');
break;
default:
if($vScore < -7){ $vClass = 7; } else { $vClass = abs($vScore); }
printf($phrase, abs($vScore) . ' Dislikes', 'v_m' . $vClass . ' hint--error');
}
}
} else {
return;
}
}
?>
displayCodes.php
<?php
function displayCodesMain($page){
if($page == 1){?>
<table class="friendcodes">
<tr>
<th>Username</th>
<th>Friend Code</th>
<th>Affinity</th>
<th>Level</th>
<th>Rating</th>
</tr>
<?php
$plisting = file_get_contents('plist.txt');
$plist = explode(' ', $plisting, 4);
require_once('vote.php');
array_pop($plist);
foreach($plist as $item) {
$item = explode(':', $item);
echo '<tr class="fc_premium">';
$item[3] = substr($item[3], 0, 3) . '-' . substr($item[3], 3, 7);
$item[1] = str_replace('&', ' ', $item[1]);
echo '<td>' . $item[1] . '</td><td>' . $item[3] . '</td><td style="text-transform:capitalize;">' . $item[4] . '</td><td>' . $item[5] . '</td><td> <div class="bc"><div class="vote v_p7"> </div></div> </td>';
echo '</tr>';
}
$listing = file_get_contents('list.txt');
$list = explode(' ', $listing, 26);
array_pop($list);
foreach($list as $item) {
$item = explode(':', $item);
echo '<tr class="fc_regular">';
$item[3] = substr($item[3], 0, 3) . '-' . substr($item[3], 3, 7);
$item[1] = str_replace('&', ' ', $item[1]);
echo '<td>' . $item[1] . '</td><td>' . $item[3] . '</td><td style="text-transform:capitalize;">' . $item[4] . '</td><td>' . $item[5] . '</td><td>'. displayRating($item[0]) .'</td>';
echo '</tr>';
}
echo '</table>';
} else if($page == 2) {
// Start page #2 management
?>
<table class="friendcodes">
<tr>
<th>Username</th>
<th>Friend Code</th>
<th>Affinity</th>
<th>Level</th>
</tr>
<?php
$listing = file_get_contents('list.txt');
$list = explode(' ', $listing, 51);
$list = array_slice($list, 25, 25);
array_pop($list);
foreach($list as $item) {
$item = explode(':', $item);
echo '<tr class="fc_regular">';
$item[3] = substr($item[3], 0, 3) . '-' . substr($item[3], 3, 7);
$item[1] = str_replace('&', ' ', $item[1]);
echo '<td>' . $item[1] . '</td><td>' . $item[3] . '</td><td style="text-transform:capitalize;">' . $item[4] . '</td><td>' . $item[5] . '</td>';
echo '</tr>';
}
?>
</table>
<?php
}
} // Close the function
?>
Test.php (What I'm using to dynamically get values and whatnot - warning, contains some CSS, which you can safely skip over.
<?php
/*include('newUser.php');
addNewUser('1', 'true', 'Spitfire', '8JX-UKR8', 'Spirit', '90');*/
require('displayCodes.php');
displayCodesMain(1);
?>
<head>
<style>
/*! Hint.css - v1.3.2 - 2014-05-18
* http://kushagragour.in/lab/hint/
* Copyright (c) 2014 Kushagra Gour; Licensed MIT */
.hint,[data-hint]{position:relative;display:inline-block}.hint:before,.hint:after,[data-hint]:before,[data-hint]:after{position:absolute;-webkit-transform:translate3d(0,0,0);-moz-transform:translate3d(0,0,0);transform:translate3d(0,0,0);visibility:hidden;opacity:0;z-index:1000000;pointer-events:none;-webkit-transition:.3s ease;-moz-transition:.3s ease;transition:.3s ease;-webkit-transition-delay:0ms;-moz-transition-delay:0ms;transition-delay:0ms}.hint:hover:before,.hint:hover:after,.hint:focus:before,.hint:focus:after,[data-hint]:hover:before,[data-hint]:hover:after,[data-hint]:focus:before,[data-hint]:focus:after{visibility:visible;opacity:1}.hint:hover:before,.hint:hover:after,[data-hint]:hover:before,[data-hint]:hover:after{-webkit-transition-delay:100ms;-moz-transition-delay:100ms;transition-delay:100ms}.hint:before,[data-hint]:before{content:'';position:absolute;background:transparent;border:6px solid transparent;z-index:1000001}.hint:after,[data-hint]:after{content:attr(data-hint);background:#383838;color:#fff;text-shadow:0 -1px 0 #000;padding:8px 10px;font-size:12px;line-height:12px;white-space:nowrap;box-shadow:4px 4px 8px rgba(0,0,0,.3)}.hint--top:before{border-top-color:#383838}.hint--bottom:before{border-bottom-color:#383838}.hint--left:before{border-left-color:#383838}.hint--right:before{border-right-color:#383838}.hint--top:before{margin-bottom:-12px}.hint--top:after{margin-left:-18px}.hint--top:before,.hint--top:after{bottom:100%;left:50%}.hint--top:hover:after,.hint--top:hover:before,.hint--top:focus:after,.hint--top:focus:before{-webkit-transform:translateY(-8px);-moz-transform:translateY(-8px);transform:translateY(-8px)}.hint--bottom:before{margin-top:-12px}.hint--bottom:after{margin-left:-18px}.hint--bottom:before,.hint--bottom:after{top:100%;left:50%}.hint--bottom:hover:after,.hint--bottom:hover:before,.hint--bottom:focus:after,.hint--bottom:focus:before{-webkit-transform:translateY(8px);-moz-transform:translateY(8px);transform:translateY(8px)}.hint--right:before{margin-left:-12px;margin-bottom:-6px}.hint--right:after{margin-bottom:-14px}.hint--right:before,.hint--right:after{left:100%;bottom:50%}.hint--right:hover:after,.hint--right:hover:before,.hint--right:focus:after,.hint--right:focus:before{-webkit-transform:translateX(8px);-moz-transform:translateX(8px);transform:translateX(8px)}.hint--left:before{margin-right:-12px;margin-bottom:-6px}.hint--left:after{margin-bottom:-14px}.hint--left:before,.hint--left:after{right:100%;bottom:50%}.hint--left:hover:after,.hint--left:hover:before,.hint--left:focus:after,.hint--left:focus:before{-webkit-transform:translateX(-8px);-moz-transform:translateX(-8px);transform:translateX(-8px)}.hint--error:after{background-color:#b34e4d;text-shadow:0 -1px 0 #592726}.hint--error.hint--top:before{border-top-color:#b34e4d}.hint--error.hint--bottom:before{border-bottom-color:#b34e4d}.hint--error.hint--left:before{border-left-color:#b34e4d}.hint--error.hint--right:before{border-right-color:#b34e4d}.hint--warning:after{background-color:#c09854;text-shadow:0 -1px 0 #6c5328}.hint--warning.hint--top:before{border-top-color:#c09854}.hint--warning.hint--bottom:before{border-bottom-color:#c09854}.hint--warning.hint--left:before{border-left-color:#c09854}.hint--warning.hint--right:before{border-right-color:#c09854}.hint--info:after{background-color:#3986ac;text-shadow:0 -1px 0 #193b4d}.hint--info.hint--top:before{border-top-color:#3986ac}.hint--info.hint--bottom:before{border-bottom-color:#3986ac}.hint--info.hint--left:before{border-left-color:#3986ac}.hint--info.hint--right:before{border-right-color:#3986ac}.hint--success:after{background-color:#458746;text-shadow:0 -1px 0 #1a321a}.hint--success.hint--top:before{border-top-color:#458746}.hint--success.hint--bottom:before{border-bottom-color:#458746}.hint--success.hint--left:before{border-left-color:#458746}.hint--success.hint--right:before{border-right-color:#458746}.hint--always:after,.hint--always:before{opacity:1;visibility:visible}.hint--always.hint--top:after,.hint--always.hint--top:before{-webkit-transform:translateY(-8px);-moz-transform:translateY(-8px);transform:translateY(-8px)}.hint--always.hint--bottom:after,.hint--always.hint--bottom:before{-webkit-transform:translateY(8px);-moz-transform:translateY(8px);transform:translateY(8px)}.hint--always.hint--left:after,.hint--always.hint--left:before{-webkit-transform:translateX(-8px);-moz-transform:translateX(-8px);transform:translateX(-8px)}.hint--always.hint--right:after,.hint--always.hint--right:before{-webkit-transform:translateX(8px);-moz-transform:translateX(8px);transform:translateX(8px)}.hint--rounded:after{border-radius:4px}.hint--no-animate:before,.hint--no-animate:after{-webkit-transition-duration:0ms;-moz-transition-duration:0ms;transition-duration:0ms}.hint--bounce:before,.hint--bounce:after{-webkit-transition:opacity .3s ease,visibility .3s ease,-webkit-transform .3s cubic-bezier(0.71,1.7,.77,1.24);-moz-transition:opacity .3s ease,visibility .3s ease,-moz-transform .3s cubic-bezier(0.71,1.7,.77,1.24);transition:opacity .3s ease,visibility .3s ease,transform .3s cubic-bezier(0.71,1.7,.77,1.24)}
.vote { background: url('vote.png') no-repeat top left; width: 20px; height: 20px; }
.vote.v_m7 { background-position: 0px 0px; }
.vote.v_p7 { background-position: 0px -30px; }
.vote.v_p6 { background-position: 0px -60px; }
.vote.v_m6 { background-position: 0px -90px; }
.vote.v_m5 { background-position: 0px -120px; }
.vote.v_p5 { background-position: 0px -150px; }
.vote.v_p4 { background-position: 0px -180px; }
.vote.v_m4 { background-position: 0px -210px; }
.vote.v_m3 { background-position: 0px -240px; }
.vote.v_p3 { background-position: 0px -270px; }
.vote.v_p2 { background-position: 0px -300px; }
.vote.v_m2 { background-position: 0px -330px; }
.vote.v_m1 { background-position: 0px -360px; }
.vote.v_p1 { background-position: 0px -390px; }
.vote.v_equal { background-position: 0px -420px; }
.bc {
display: inline-block;
width: 20px;
height: 20px;
padding: 1px;
border-radius: 12px;
border: 1px solid #888;
}
.friendcodes {
border-spacing: 0;
}
.friendcodes tr > td {
padding: 5px 25px;
}
.fc_premium {
background: #ffdf8e;
border-spacing: 0;
}
.fc_regular {
border-spacing: 0;
}
</style>
</head>
<body>
<h3>
Use form below for Adding IDs
</h3>
<form action="http://shortcutcentral.org/projects/friendcodes/receiveQuery.php" method="post">
<input type='text' value="481" name="id" /><br>
<input type="submit" />
</form>
<h3>
Current Testing Stuff
</h3>
<p>
<?php
require('vote.php');
displayRating(481);
?>
</p>
</body>
Here's the exact error for those of you who are keen on it - I've obfuscated some of the stuff here as well, since I am working from a live install:
Fatal error: Cannot redeclare displayRating() (previously declared in /home/**/public_html/**/vote.php:2) in /home/**/public_html/**/vote.php on line 68
Both displayCodes.php and test.php are requiring vote.php, and the later is not doing a require_once. Switch test.php to require_once('vote.php');
It seems that this is ocurring because in your test.php code you are including vote.php with
require('vote.php');
and also you are requiring 'displayCodes.php' in test.php
The problem here is that you also call 'displayCodes.php' inside vote.php so the function is being declared two times. Try commenting this line in test.php
require('displayCodes.php');
so that way you are just including the function once.
Hope this helps
Try to remove the require_once('medoo.min.php'); in displayRating() method. You're caling displayRating() to a loop so it probably iclude medoo.min.php many times.

The first result of search term did not come out

Why always do the first result of my search term will not come out?
I noticed that it will only appear when I navigate to the page number 0.
How can I solve this?
I hope that when I navigate to page number 1, the first result will come out too!
PHP Code:
<?php
//php code goes here
include 'connect.php'; // for database connection
include 'script_suggestion.php';
include 'script_close_suggestion_box.php';
$query = $_GET['q']; // query
$button = $_GET ['submit'];
if (isset($_GET['page'])) {
$page_number = (int)$_GET['page'];
$page_number = mysqli_real_escape_string($page_number);
} else {
$page_number = 1;
}
$results_per_page = 10;
?>
HTML Code:
<html>
<head>
<title>
Brandon's Search Engine
</title>
<style type="text/css">
#title a {
font-size: 17pt;
margin: 5px;
padding: 2px;
border-color: black;
text-decoration: underline;
width: 544px;
}
#search-result {
display: block;
border: 1px solid grey;
border-color: grey;
}
#search-result:hover {
background-color: #dddddd;
width: 544px;
}
#link {
font-size: 17pt;
margin: 5px;
padding: 2px;
width: 544px;
}
#description {
font-size: 17pt;
margin: 5px;
padding: 2px;
width: 544px;
}
#search-page-number {
display: block;
width: auto;
height: auto;
border: 1px solid gray;
margin: 2px;
padding-left: 5px;
padding-right: 5px;
padding-bottom: 2px;
padding-top: 2px;
list-style: none;
float: left;
text-align: center;
}
#search-page-number:hover {
background-color: #dddddd;
}
#suggestion {
border: 1px solid black;
visibility: hidden;
position: absolute;
background-color: white;
z-index: 10;
}
#suggestion a {
font-size: 12pt;
color: black;
text-decoration: none;
display: block;
width: 548px;
height: auto;
text-align: left;
padding: 2px;
}
#suggestion a:hover {
background-color: #dddddd;
width: 544px;
padding: 2px;
}
</style>
</head>
<body>
<form method="GET" action="search.php">
<table>
<tr>
<td>
<h2>
Brandon's Search Engine
</h2>
</td>
</tr>
<tr>
<td>
<input type="text" value="<?php echo htmlspecialchars($_GET['q']); ?>" name="q" style="height: 27px; width: 550px; padding: 2px" name="q"
onkeyup="getSuggestion(this.value)" autocomplete="off" onblur="closeBox()" placeholder="Search Now"/>
<input type="submit" value="Search" style="height: auto; width: 60px; padding: 2px" />
<div id="suggestion" style="width: 548px">
</div>
</td>
</tr>
</table>
<br>
<hr>
<table>
<tr>
<td>
<?php
//count
$count_sql = "SELECT count(*) as c FROM searchengine WHERE title LIKE '%" . mysqli_real_escape_string($con,$query) . "%' OR keywords LIKE '%" . mysqli_real_escape_string($con,$query) . "%' OR link LIKE '%" . mysqli_real_escape_string($con,$query) . "%' ";
$search_count = mysqli_fetch_array(mysqli_query($con,$count_sql));
$number_of_result = $search_count['c'];
//SQL query
$page_number = (int)$_GET['page'];
$stmt = "SELECT * FROM searchengine WHERE title LIKE '%" . mysqli_real_escape_string($con,$query) . "%' OR keywords LIKE '%" . mysqli_real_escape_string($con,$query) . "%' OR link LIKE '%" . mysqli_real_escape_string($con,$query) . "%' LIMIT " . $page_number . " , $results_per_page";
$result = mysqli_query($con,$stmt) or die(mysqli_error($con));
//$number_of_result = mysqli_num_rows($result);
if ($number_of_result < 1) {
echo "<b>No results found!</b>";
echo "<p>";
echo "Your search - <b>$query</b>" . " - did not match any documents. Please try different keywords.";
}
elseif ($number_of_result > 1) {
echo "<b>$number_of_result results found!</b>";
echo "<p>";
//results found here and display them
while (($row = \mysqli_fetch_array($result))) {//10 results per page
$title = $row["title"];
$description = $row["description"];
$link = $row["link"];
echo "<div id='search-result'>";
echo "<div id='title'><a href='$link'>" . $title . "</a></div>";
//echo "<br />";
echo "<div id='link'><small>" . $link . "</small></div>";
//echo "<p>";
echo "<div id='description'><small>" . $description . "</small></div>";
echo "</div>";
echo "<br />";
}
}
elseif ($number_of_result == 1) {
echo "<b>$number_of_result result found!</b>";
echo "<p>";
//results found here and display them
while (($row = \mysqli_fetch_array($result))) {//10 results per page
$title = $row["title"];
$description = $row["description"];
$link = $row["link"];
echo "<div id='search-result'>";
echo "<div id='title'><a href='$link'>" . $title . "</a></div>";
//echo "<br />";
echo "<div id='link'><small>" . $link . "</small></div>";
echo "<br />";
echo "<div id='description'><small>" . $description . "</small></div>";
echo "</div>";
echo "<br />";
}
}
?>
</td>
</tr>
<tr>
<td>
<input type="hidden" name="page" value="<?php echo 1; ?>" />
<div id="page-number">
<?php
$max_page_number = ceil($number_of_result / $results_per_page);
if ($number_of_result == 0) {
echo " ";
}
elseif ($number_of_result != 0) {
echo "You are on page $page_number of $max_page_number.";
}
//ie if 35 results are therer then we require 4 pages that are 0 to max_page_number
//current page number is equal to page_number
//echo $max_page_number;
echo "<ul>";
//both the condition are not the neccesary
if ($max_page_number >= 1) { // if more than 2 pages
if ($page_number != 1) { //First
echo "<li id='search-page-number'>";
echo "<a href=search.php?q=$query&page=1>First</a>";
echo "</li>";
}
if ($page_number != 1) { //Previous
echo "<li id='search-page-number'>";
$prev = $page_number - 1;
echo "Previous";
echo "</li>";
}
for($index = 1 ; $index <= $max_page_number ; $index++) {
echo "<li id='search-page-number'>";
echo "<a href=search.php?q=$query&page=".($index).">";
echo ($index) . "</a>";
echo "</li>";
}
if ($page_number != $max_page_number) { //Next
echo "<li id='search-page-number'>";
$next = $page_number + 1;
echo "Next";
echo "</li>";
}
if ($page_number != $max_page_number) { //Last
echo "<li id='search-page-number'>";
echo "Last";
echo "</li>";
}
} elseif (($max_page_number == 1 ) ) {
echo "<li id='search-page-number'>";
echo "<a href=search.php?q=$query&page=1>1</a>";
echo "</li>";
}
echo "</ul>";
?>
</div>
</td>
</tr>
<tr>
<td align="center">
To insert your site in result fill in the form at here.
</td>
</tr>
</table>
</form>
</body>
</html>
Your query ends with:
"LIMIT " . $page_number . ", $results_per_page"
But that's not how the LIMIT clause works in SQL. It's defined to be:
LIMIT offset, row_count
offset is a row number, starting from 0. So if you do:
LIMIT 1, 10
You'll get records 2 through 11, not 1 through 10. The correct way to do it is:
"LIMIT " . ($page_number-1)*$results_per_page . ", $results_per_page"
Something else you should find useful is the SQL_CALC_FOUND_ROWS option to the SELECT statement. This tells MySQL to calculate how many total rows matched the criteria, even though you used LIMIT to return just a subset. This way you don't have to do two queries, one with COUNT(*) and another to get a page of data. After doing a query with this option, you can use SELECT FOUND_ROWS() to get the full count.
$safequery = mysqli_real_escape_string($con,$query);
$stmt = "SELECT SQL_CALC_FOUND_ROWS *
FROM searchengine
WHERE title LIKE '%" . $safequery . "%'
OR keywords LIKE '%" . $safequery . "%'
OR link LIKE '%" . $safequery . "%'
LIMIT " . ($page_number-1)*results_per_page . " , $results_per_page";
$result = mysqli_query($con,$stmt) or die(mysqli_error($con));
$count_sql = "SELECT FOUND_ROWS() AS c";
$search_count = mysqli_fetch_array(mysqli_query($con,$count_sql));
$number_of_result = $search_count['c'];

Multicolored divs

my site which is a search engine returns many many results with a foreach loop as such:
foreach ($xml->channel->item as $result) {
$ltitle = $result->title;
$ldesc = $result->description;
$url = $result->displayUrl;
$link = $result->link;
if (strlen($ltitle) > 60)
{
$title = substr($ltitle,0,60).'...' ;
}
else
{
$title = $ltitle;
}
if (strlen($ldesc) > 195)
{
$desc = substr($ldesc,0,195).'...' ;
}
else
{
$desc = $ldesc;
}
echo "
<br>
<div class='resultbox'>
<a class='normal' style='text-decoration:none;font-size:huge;font-weight:bold' href='$link'>$title</a><br>
<div style='padding-top:3px;padding-bottom:4px;width:580px;'>
<font style='text-decoration:none;font-size:small;font-family:Arial;'>$desc<br></font></div>
<a style='text-decoration:none;' href='$link'><font style='text-decoration:none;font-size:small;color:green;font-weight:bold;'>$url<br></font></a>
</div>
";
}
And the resultbox class above styles all of the results with this
.resultbox
{
height:auto;
width:600px;
background-color:transparent;
font-size:19px;
padding:10px;
padding-left: 30px;
padding-right: 30px;
border-left: 6px solid #333;
}
.resultbox:hover
{
border-left: 8px solid #555;
}
The border-left color is what i want changed, i would like it to generate or to style randomly off of a list of colour codes so the results, insead of being all #333 can be #333 #555 #999 and so on..... any ideas?
If u have no problems using JS , You can certainly do this :
$(document).ready(function () {
$('.resultbox').mouseenter(function() {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
$('.resultbox').css("border-left", " 8px solid #"+randomColor);
});
});
change <div class='resultbox'> to <div class='resultbox random-color-".rand(1,YOUR_COLOR_LIMIT)."'> AND define colors like
.random-color-1 {
border-left: 8px solid #555;
}
.random-color-2 {
border-left: 8px solid #555;
}
.....
.random-color-YOUR_COLOR_LIMIT {
border-left: 8px solid #555;
}
change
<div class='resultbox'>
to
<div class='resultbox' style='border-left-color:$yourColorInCssFormat;'>
the style attribute overrides the css from class.
set $yourColorInCssFormat to the color you wish to have for the div. for example: $yourColorInCssFormat = '#999';
You can use inline style for that. Or alternatively you can user nth-child selector of css to repeat the border-color scheme something like this:
.resultbox:nth-child(n+1):hover {
}
.resultbox:nth-child(2n+1):hover {
}
.resultbox:nth-child(3n+1):hover {
}
First off, try this out for your foreachloop:
<?php foreach ($xml->channel->item as $result): ?>
<?php
$ltitle = $result->title;
$ldesc = $result->description;
$url = $result->displayUrl;
$link = $result->link;
if (strlen($ltitle) > 60){
$title = substr($ltitle,0,60).'...' ;
}else{$title = $ltitle;}
if (strlen($ldesc) > 195){
$desc = substr($ldesc,0,195).'...' ;
}else{$desc = $ldesc;}
?>
<div class='resultbox'>
<a class='normal' style='text-decoration:none;font-size:huge;font-weight:bold' href='<?php echo $link ?>'><?php echo $title; ?></a>
<br>
<div style='padding-top:3px;padding-bottom:4px;width:580px;'>
<font style='text-decoration:none;font-size:small;font-family:Arial;'>
<?php echo $desc; ?><br>
</font>
</div>
<a style='text-decoration:none;' href='<?php echo $link; ?>'><font style='text- decoration:none;font-size:small;color:green;font-weight:bold;'><?php echo $url; ?><br></font> </a>
<?php endforeach; ?>
That way you're not playing with big echos.
Now for generating random colors your could use php rand();
For example:
//Generate a random number between the two parameters
$randomNumber = rand(1, 3);
//Use this number to dictate what the variable color should be
if($randomNumber == 1){$color = "#333"}
elseif($randomNumber == 2){$color = "#555"}
elseif($randomNumber == 3){$color = "#999"}
You can then use the variable $color in your code to randomly assign one of the colors to elements.
Hope this helps!
-Gui

Search Criteria in PHP

JAVASCRIPT
function ShowContent(id) {
if(id.length < 1) { return; }
document.getElementById(id).style.display = "block";
}
PHP
$test = mysql_query('SELECT userid,testid,testname FROM test_table WHERE userid = 9 ORDER BY name');
TestName : <select>
while($row=mysql_fetch_array($test))
{
$testname = $row['testname'];
$testid = $row['testid'];
echo '<option value='.$testid.'>'.$testname.'</option>
}
</select>
echo '<div id="testid" style="display:none;">
echo '<table>
<tr><th>USERID</th</tr>
<tr><th>TESTNAME</th></tr>
<tr><th>MARKS</th></tr>
publishtest = mysql_query('SELECT id,userid,publishtest,marks FROM publish_table WHERE userid= 9 GROUP BY id');
while($row=mysql_fetch_array($publishtest))
{
$userid = $row['userid'];
$testid = $row['id']; /*This is test id*/
$testname = $row['publishtest']; /* This is test name*/
$marks = $row['marks'];
echo '<tr><td>'.$userid.'</td>
<td>'.testid.'</td>
<td>'.testname.'</td>
<td>'.$marks.'</td>
</tr>
}
echo '</table>';
echo '</div>';
I need when I select test from select tag, then div should be displayed of that testid. I want either in javascript, jquery
You can try this:
JavaScript
$('#showDivSelect').change(function (e) {
var divId = $(this).children('option:selected').val();
$('#' + divId).css('display', 'block');
});​
HTML
<select id="showDivSelect">
<option id="o1">Div1</option>
<option id="o2">Div2</option>
</select>
<div id="Div1"></div>
<div id="Div2"></div>​
CSS
#Div1 {
width: 30px;
height: 30px;
background-color: red;
display: none;
}
#Div2 {
width: 30px;
height: 30px;
background-color: blue;
display: none;
}​
Here is working example: http://jsfiddle.net/ePSDu/

Categories