Well I’m kind of new in this with web programing and I’m sure I’m breaking some rules but I really need a hand here. Well the idea it’s that I have created a slider with the information that I bring from the data base, so I’m working with php and I’m making that a piece of code in php fills all the li and divs for the slider, and I’m giving the id to the div that contains all the details the name of the user of the data base, so the deal it’s that I have to have a search field that looks through all the divs I have, and after the search it only display the name of the user you are looking in the search field.
So this is the code I have of php that’s in the page, well I try to use an ajax but when the information comes from the php file it doesn’t respect the size of the div and prints all the information and I don’t get the slider.
<div class="slider" id="slider">
<?php
$cont = 0;
$bandera=0;
$tamano = 0;
$nombreUsuario = "";
$arrayIDs = array();
$i = 0;
$query_Total = "SELECT u.FOTO_HIJO_USUARIOS, u.ID_USUARIOS, u.NOMBRE_USUARIOS, u.NOMBRE_HIJO_USUARIOS, u.FB_ID_USUARIOS, m.ID_MOMENTOS_MAGICOS, m.TEXTO_MOMENTOS_MAGICOS FROM MOMENTOS_MAGICOS AS m, USUARIOS AS u WHERE u.ID_USUARIOS = m.ID_USUARIOS AND u.ESTADO_USUARIOS = '1'";
$todas_Total = mysql_query($query_Total, $MM_db) or die(mysql_error());
echo "<ul>";
while(($row = mysql_fetch_array($todas_Total, MYSQL_ASSOC))){
$nombreUsuario = $row['NOMBRE_USUARIOS'];
$arrayIDs[$i] = $nombreUsuario;
$i++;
$bandera++;
if($cont++%3==0){
echo '<li>';
}
echo '<div class="participantes" id="'.$nombreUsuario.'" style="float:left; padding-left:10px;width:145px;margin-right:25px;margin-left:15px; margin-top:15px">';
echo '<div class="face_foto"><img src="https://graph.facebook.com/'.$row['FB_ID_USUARIOS'].'/picture" width="52" height="52" /></div>';
echo '<h1 align="right">'.$nombreUsuario.'</h1>';
echo '<img class="bebe_participante" src="uploads/'.$row['FOTO_HIJO_USUARIOS'].'" width="153" height="89" />';
echo '<h2 align="center">'.$row['NOMBRE_HIJO_USUARIOS'].'</h2>';
echo '<div id="botMas"> <img class="img_votos" src="images/bt_ver_mas.png" width="89" height="35"/> </div>';
$query_Votos = "SELECT COUNT( * ) AS VOTOS FROM VOTOS WHERE ID_MOMENTOS_MAGICOS = '".$row['ID_MOMENTOS_MAGICOS']."'";
$todos_Votos = mysql_query($query_Votos, $MM_db) or die(mysql_error());
while(($row = mysql_fetch_array($todos_Votos, MYSQL_ASSOC))){
echo '<div class="votos">'.$row['VOTOS'].' VOTOS </div>';
}
echo '</div>';
$tamano++;
}
if($cont%3==0 || $cont==$bandera ){
echo '</li>';
}
echo "</ul>";
?>
</div>
So I was thinking of making the search with javascript or jquery, but after some tries I haven’t been able to make it.
So I really hope you guys can help me.
Thank you for your time :)
Use http://ivaynberg.github.com/select2/ for this.
See: Many-to-Many Ajax Forms (Symfony2 Forms) for an implementation (ignore the fact that this is tagged symfony2, it is relevant to php as well)
Related
this code work only in admin page but can't work in view page how to retrieve image from view page in codelgniter
code:
<?php
include('config.php');
$result = mysql_query("SELECT * FROM photos");
while($row = mysql_fetch_array($result))
{
echo '<div id="imagelist">';
echo '<p><img src="'.$row['location'].'"></p>';
echo '<p id="caption">'.$row['caption'].' </p>';
echo '</div>';
}
?>
Why you use mysql extension in codeigniter ?!
Even though the meaning of that code work in admin is you store your image inside admin folder
So for best practice
Please use codeIgniter as a FrameWork not a place to paste your procedural PHP code !
Also show us what is in $row['location'] ?!
I am working on building a news section for one of my sites. I am storing the information for each news article in a database table news, the main text of the article is in column copy whose type is set to longtext.
I use the following line of code to display the news article as paragraphs:
<p class="news_copy"><?php echo preg_replace('/[\r\n]+/', '</p><p class="news_copy">', $news['copy']); ?></p>
and it works absolutely perfect.
The reason for this post is because I would like to be able to display more than one photo for each news article. I am setting a lead image in the database, but if there were, say, 4 other photos in one story and 6 in another (in addition to the lead image) is there any way for me to display, say, 1 or more photo(s) per paragraph?
I'll be happy to elaborate in case this is too confusing.
Many thanks!
<?php echo insert_par_and_photos($news['copy'], $news['photos']); ?>
function insert_par_and_photos($copy, $photos) {
$pars = preg_split('/[\r\n]+/', $copy);
$result = '';
foreach ($pars as $i => $par) {
$result .= '<p class="news_copy">';
$result .= $par;
if ($i < count($photos)) {
$result .= '<img src="'+$photos[$i]+'"/>';
}
$result .= '</p>';
}
return $result;
}
I want to parse the news from the website which is : http://www.bakirkoykhb.gov.tr/?module=bizden_haberler , what i'm facing is the website has many tables without classes and very complicated and so on as you can see if you check the source code of the page, i get the headings like those ones :
Japonya-Türkiye Tıp Derneği'den Ziyaret
Saglık-Sen Gen. Başk.Memiş'ten Ziyaret
Vali Mutlu’ dan Ziyaret
and so on with my code below. but i don't have any idea that how can i parse the links which are contained under "Devamını Oku".
$html = file_get_html('http://www.bakirkoykhb.gov.tr/?module=bizden_haberler');
$j = 0;
foreach($html->find('b') as $element2){
echo "
<h3 ><a target=\"_blank\" href=\"#somethingtohere\">". $element2->plaintext . "</a></h3>
";
$j++;
if($j > 5) break;
}
Hope you can help, thank you.
This should do for you
echo "<h3 ><a href='http://www.bakirkoykhb.gov.tr/".$html->find('a',$j+22)->href."'>". $element2->plaintext . "</a></h3>";
Using Object-Oriented PHP, where should HTML be rendered?
The business processes include several actions to maintain customer records.
Should the rendering of each business process get a separate PHP file? ie. viewCustomerTransactions.php?
Where should code like this reside?
$custTrans = Customer.getTransactions();
foreach ($custTrans as $ct){
$amount = $ct[0];
$date = $ct[1];
$product = $ct[2];
echo '<div class="custTrans">';
echo '<span class="custTransAmount">'.$amount.'</span>';
echo '<span class="custTransDate">'.$date.'</span>';
echo '<span class="custTransproduct">'.$product.'</span>';
echo '</div>';
}
Perhaps an MVC framework like codeigniter would be better?
I'm still figuring out what's the best way to keep php and layout seperate without too much fuzz. For the moment I really like the include-templating approach, beacause it's so simple and has no restrictions.
So, for your example, you would have a php file (example.php) that looks like this:
<?php
$custTrans = Customer.getTransactions();
$displ_transactions = array();
foreach ($custTrans as $ct){
$transaction = array(
'amount' => $ct[0],
'date' => $ct[1];
'product' => $ct[2];
);
$displ_transactions[] = $transaction; // this will push the transacion into the array
}
include 'example.tpl.php'
?>
And then you need a second file (example.tpl.php):
<?php foreach ($displ_transactions as $transaction) { ?>
<div class="custTrans">
<span class='custTransAmount'><?php echo $transaction['amount'] ?></span>;
<span class='custTransDate'><?php echo $transaction['date'] ?></span>;
<span class='custTransproduct'><?php echo $transaction['product'] ?></span>;
</div>
<?php } ?>
Just call example.php in your browser and you will see the same result as you had before.
This is all good and well for small websites, because this method causes some overhead. If you are serious about templating, use smarty. it's easy to learn, and it has automatic caching, so it's super fast.
I just realize you can also do it this way:
example.php:
<?php
$custTrans = Customer.getTransactions();
foreach ($custTrans as $ct){
$amount = $ct[0];
$date = $ct[1];
$product = $ct[2];
include 'example.tpl.php';
}
?>
example.tpl.php:
<div class="custTrans">
<span class='custTransAmount'><?php echo $amount ?></span>;
<span class='custTransDate'><?php echo $date ?></span>;
<span class='custTransproduct'><?php echo $product ?></span>;
</div>
Use whatever suits you best :)
If I were you I would store the html in a variable instead of echoing it out like so:
$custTrans = Customer.getTransactions();
$html = "";
foreach ($custTrans as $ct){
$amount = $ct[0];
$date = $ct[1];
$product = $ct[2];
$html .= "<div class="custTrans">";
$html .= "<span class='custTransAmount'>".$amount."</span>";
$html .= "<span class='custTransDate'>".$date."</span>";
$html .= "<span class='custTransproduct'>".$product."</span>";
$html .= "</div>";
}
You then have this html data stored in the variable $html and you can echo it out where ever you like.
echo $html;
Does that solve you problem mate?
W.
I would have to confirm that the include-templating (mentioned by Jules Colle) is one of the answers, nevertheless, it might get messy to maintain when the project is too large, so keep in mind to document what file is included by what file, since there is (currently) no (free) IDE solution to this type of chaining... a solution that would easily bring you from one file to another or simply lay-out everything into a procedural-like code.
Edit:
do not forget the magic constants:
http://www.php.net/manual/en/language.constants.predefined.php
and this:
$_SERVER['DOCUMENT_ROOT']
Does anyone know how to bring in all of a mysql tables' results, only show the first X, (say 10), and then hide the rest using jquery? Basically, as I've already got the jquery, I just need to know how to show only the first X results in one div, then the rest in a seperate div.
My aim is to only show the first 10 results, but provide a link at the bottom of the page allowing the user to show all of the results. Was thinking the hyperlink could just re-execute the query but thought it would be easier to show/hide using jquery.
Many thanks in advance. S
Thought I'd add the code I'm using below
$query = "SELECT * FROM ispress WHERE active = '1' ORDER BY YEAR(date) DESC, MONTH(date) DESC LIMIT 0, 7";
$resultSet = mysql_query($query);
if (mysql_num_rows($resultSet))
{
$newsArray = array();
while ($newsResult = mysql_fetch_array($resultSet))
{
$newDate = $newsResult['date'] ;
$timePeriod = date('F Y ',strtotime($newDate));
$bFirstTime = true;
if (!isset($newsArray[$timePeriod]))
{
$newsArray[$timePeriod] = array();
}
$newsArray[$timePeriod][] = $newsResult;
}
foreach ($newsArray as $timePeriod => $newsItems)
{
echo '<div class="date">' . $timePeriod . '</div>' . PHP_EOL;
echo '<ul class="press">' . PHP_EOL;
foreach ($newsItems as $item)
{
if ($bFirstTime) {
echo '<li>';
echo '<img src="'.$wwwUrl.'images/news/'.$item['image'].'" width="'.$item['imgWidth'].'" height="'.$item['imgHeight'].'" title="'.$item['title'].'" alt="'.$item['title'].'" />
<h3>'.$item["title"].'</h3>
<p>'.substr($item['descrip'],0,244).'...</p>
<p>Read more</p>
';
echo '</li>' . PHP_EOL;
$bFirstTime = false;
} else {
echo '<li>';
echo '<img src="'.$wwwUrl.'images/news/'.$item['image'].'" width="'.$item['tnWidth'].'" height="'.$item['tnHeight'].'" title="'.$item['title'].'" alt="'.$item['title'].'" />
<h3>'.$item["title"].'</h3>
<p>'.substr($item['descrip'],0,100).'...</p>
<p>Read more</p>
';
echo '<div class="clear"></div>' . PHP_EOL;
echo '</li>' . PHP_EOL;
}
}
echo '</ul>' . PHP_EOL;
}
echo '<p>Older posts...</p>'. PHP_EOL;
echo '<div id="slickbox">This is the box that will be shown and display the rest of the news results. :)</div>'. PHP_EOL;
}
else
{
echo 'We currently have no press releases available';
}
This will hide the first 10 children. How are you planning on showing the other results? Buttons, fields, jqueryui widgets?
You will just need to add a click event which calls this function.
function limit_results(start, end) {
$('#things > .thing').each(index) {
if(index < end && index >= start) {
$(this).hide();
}
}
}
limit_results(1,10);
If you have your elements in a jQuery object already (say, $('#sql-results') holds all of your results), you can always do this: $('#sql-results:lt(10)') to work with the first ten elements, and $('#sql-results:gt(9)') to work with the rest of the elements.
You have to decide yourself how efficient your approach is for this amount of data you're processing.
Right, so for your specific markup structure, you can add this to your JS:
// Obviously this is untested and probably not bug-/typo-free
(
function($) {
var $slickbox = $('#slickbox').hide();
$('<ul></ul>')
.appendTo($slickbox)
.append('ul.press li:gt(9)');
$('#slick-toggle')
.bind(
'click',
function(){
$slickbox.toggle();
}
);
}
)(jQuery);
This would involve a lot of rewriting but jquery has a datatables plugin that will display the data. To use it you need to do something like
echo '<table id="news-table">'
echo '<thead>';//Datatables needs a thead with the correct number of columns. However you don't need to fill them in.
echo '<th>Date</th>';
echo '<th>Time Period</th>'
echo '</thead><tbody>';
while ($data = my_sql_fetch_array($result)) {
echo '<td>Whatever</td>';
echo '<td>Another Field</td>';
}
echo '</tbody></table>';
The jquery is then
$('#news-table').dataTable();
I'm not sure how it would do custom no data messages and I know that with the code you have written this may not be any good to you right now but I'm posting it because it could be useful for somebody looking for pagination info or for you if you want to do something similar again. Datatables is also useful because the user can choose the number of results they want to show, what column they want to sort by and what direction to sort in.
in your query
limit 0,10
for the rest
limit 11,xxx
When you print out each row's data count each iteration by incrementing a counter. When you get to 11 start a new div that has a different id to that of your 1st div that you already defined an id for. Now using jQuery you can hide and show the 2nd div with the remaining results as you please.
Divide the return values in your php file with a character
ex:
echo "this is first value +";
echo "this is second value +";
echo "this is third value +";
use javascript to separate the return values
ex:
var ajaxArray = ajaxValues.split("+");
now all three values are placed in ajaxArray and you may use anyone you want
ex:
ajaxArray[0] = this is first value