For loop in different pages - php

I'll try to explain the situation as clearly as I can.
I've got a system in place that presents links to events, created with JEvents for those familiar(in Joomla)
When an event is created in the admin, the category view for it displays a little table, with the event`s date, name, and a link to the event page. All that works fine. Now the link is called on a separate php file than the one that renders the html view for it all.
The link is called with this:
<a class="ev_link_row" href="<?php echo $rowlink; ?>" <?php echo $args;?> style="font-weight:bold;color:<?php echo $fgcolor;?>;" title="<?php echo JEventsHTML::special($row->title()) ;?>"><?php echo $tmpTitle ;?></a>
On the html rendering script, I've got this:
$this->viewEventRowNew ( $row,'view_detail',JEV_COM_COMPONENT, $Itemid);
Which is comprised in a for loop above, populating the $row variable with all sorts of info.
for( $r = 0; $r < $num_events; $r++ ){
$row = $data['rows'][$r];
etc.
Now to the question. I've got a table that stores imagefile names. I have no trouble creating a forloop that goesthrough the rows and returns the path name to the images, like this:
$db = & JFactory::getDBO();
$query2 = "SELECT `c`.`filename` AS `iname` , `c`.`filetitle` AS `ititle` , `c`.`ev_id` AS `eventid` FROM `#__jev_files` AS `c` ";
$db->setQuery($query2);
$imagelist = $db->loadObjectList();
?>
<?php foreach($imagelist as $row) : ?>
<img src="/images/stories/jevents/<?php echo $row->iname; ?>" />
<?php endforeach; ?>
Now the obvious question, how do I assign this image to be comprised in the link. I tried wrapping the for loop around the linkitself, but it obviously breaks hell as it is reused in a loop(creating a loop within a loop and destroying my layout)
If I use the query in the html-rendering page, then my problem is essentially that I do not know how to append the above to the $row variable without breaking the rest of the functionality.
Thanks, a LOT to anyone who can guide me on this.

I think I could give just the answer you're looking for if I could understand the question better. Perhaps some of what you're saying is Joomla-specific so I don't quite get it.
But generally, object-oriented programming is a good way to keep information about one related thing together, such as all date, description, and collection of images associated with one event. If you can loop through a set of id's and each ID can be used to create an Event object, then each Event object can be made to retrieve it's own information and related images. It keeps code like this cleaner.

Related

Building a ScreenShot Gallery with Twitter Bootstrap

I am currently in the process of trying to build a screenshot gallery. I have complete the first step which is to make it possible for the user to upload the image to a mysql database.
That is how the images for the gallery are supplied to the gallery script. Unfortunately, I have spent months perfecting my ability to get various things into a database, but not so much time on how to take them from the database and display in a user friendly format.
Twitter Bootstrap Fluid Grid System
Twitter Bootstrap utilizes a fluid grid system to display data. You don't have to use it, but it makes for a much better looking layout. Now the screenshot gallery needs to display rows of images and each row is 4 columns wide. Now here is the kicker, This is the structure for each row.
http://pastebin.com/HvBK5rTC
I need to generate that row over and over. But I need the images that populate it to cease at 4, echo the appropriate closing divs, echo the opening divs for another row, and the echo four more images out, ect, ect.
I don't have any code for this because I do not know where to start. I can supply anything that you need if you can just help me get started.
Edit
This is what I have come up with so far, but I do not know how to close the <div class="row-fluid"> and open a new one.
http://pastebin.com/EADji2eh
You are pretty much there. mysql_* fn's are deprecated, you should use PDO etc.
You can do what you need by adding an outer loop for flow control, and using what you had already as an inner. The result array, it will automatically iterate through the result set for you without needing to keep track of offsets for limits etc.
build_gallery_fn.php:
function image_from_database() {
// use a running counter for flow control of the outer loop
$runningcount = 0;
$r = mysql_query("SELECT DISTINCT logo AS `img` FROM NewsArticles WHERE logo IS NOT NULL AND logo !='';");
//Get # of images in results to use as upper limit for outer loop
$img_ct = mysql_num_rows($r);
while ($runningcount <= $img_ct){
//rowcount reset to break content blocks
$rowcount = 0;
echo "<div class=\"row-fluid\">";
$runningcount++;
while(($rowcount <= 3) && $row = mysql_fetch_assoc($r))
{
$rowcount++;
$img = $row['img'];
echo '<div class="span2">';
echo "<span class='thumbnail'><img src='attachments/logos/$img'/> Photo # $runningcount of $img_ct</span>";
echo '</div>';
}
echo "</div>";
}
}
Output Holder:
<div class="container">
<h1 class="text-center">The American Pacific Group ScreenShot Gallery</h1>
<hr>
<?php
echo image_from_database();
?>
</div>

Jquery <div> Slider with php generated <divs>

I'm pretty new to programming, and i'm writing a pretty simple website for an outlet, with 4 pages: home, beauty, wellbeing, contacts.
This website will be a showroom for this outlet's articles, which will be shown in the two section of the website, depending on their area of interest.
The contractor wanted of course to be able to upload articles on his own when he wanted, so I build a simple application in php, with a login, an administration page, where he could just write Title, price, description and upload an image for the article.
All is all right, The php program is ok, uploads go smooth, the page with the articles are automatically generated by the php program, which takes the data from the mysql database, via a while cycle.
The last problem which I cannot solve, is a Slider in the homepage.
The same identical in the article's pages, have to be shown in a in home page, but not one under the other, instead they must scroll in a div, and of course the article's divs are generated by the php program.
Now, i'm not all that great in javascript, so I don't know if I'm doing it the right way, or if something's wrong with the jquery plugin I use.
The code is this:
In the header:
<script src="js/jquery.jshowoff.min.js" type="text/javascript"></script>
And in the Body:
<div id="vetrina">
<?php
include "database.php";
$connection = Database::getConnection();
$query = ("SELECT * FROM `articoli` WHERE (`importante_art` = 'si') ORDER BY `id_art` DESC");
$ris = $connection->query($query) or die (mysqli_error())
$row_cnt = $ris->num_rows;
if(($row_cnt) > 0){
$row = "";
while ($row = mysqli_fetch_array($ris)){
$id_post = $row['id_art'];
$titolo_post = stripslashes($row['titolo_art']);
$testo_post = stripslashes($row['descrizione_art']);
$immagine_post = $row['immagine_art'];
$prezzo_post = $row['prezzo_art'];
echo '<div class="evidenziato">';
echo '<h4 class="titolo_evidenziato">'.$titolo_post.'</h4>';
echo '<p class="prezzo">'.$prezzo_post.'</p>';
echo '<img class="img_evidenziato" src='.$immagine_post.'></img>';
echo '<p class="descrizione_articolo">' . $testo_post . '</p>';
echo '</div>';
}
}else{
echo "Right now there are no articles to display";
}
?>
</div>
<script type="text/javascript">
$(document).ready(function(){ $('#vetrina').jshowoff(); });
</script>
I used the showoff plugin, that should do what i want.
http://ekallevig.com/jshowoff/
I don't know what I'm doing wrong... :\
I even tried to do the slideshow without the php program, by simpli typing in the articles, but nothing.
I tried 5 different plugins.
Maybe it's just that i'm doing something wrong at the base.
Anyway, my divs come out all right, i see that they are all there, but they just wont slide...
as mentioned at http://ekallevig.com/jshowoff/ site
for jshowoff required two files.
1). the jQuery Core JavaScript Library (1.3+)
2). jquery.jshowoff.min.js
Did you used both files?.

Drupal 7. In 'node.tpl.php' how would I limit and offset the results?

Apologies, novice Drupal question warning:
Aim: to split my outputted list of custom field images up so I can use them differently.
ie. In my node.tpl.php use the first 3 (most recent node) images in a slider. Then list the rest (offsetting the first three) just as plain images.
But I can't find how to limit my results. In my node template I can style all my images up using something like this:
<a href="<?php $nodeurl = url('node/'. $node->nid); echo $nodeurl; ?>"
<img src="<?php print render (file_create_url($node->field_featured_image['und'][0]['uri'])); ?>" alt="<?php echo $title; ?>" />
</a>
But how do I limit the result? I guess this would work to ignore the first 3 results:
<?php foreach($node->field_featured_image as $k=>$v){
if ($k < 3) continue;
print render (file_create_url($node->field_featured_image['und'][0]['uri']));
}
?>
But not in node.tpl as node.tpl appears to be a loop so the above code gets looped and the array key never gets past 0.
I think :/
Am I way off here with a basic misunderstanding of the templates?
Any pointers appreciated. Desperately trying to stick with drupal but finding it tough.
I might be mislead by what you're trying to accomplish, but you probably don't need to be hacking into node.tpl.php just yet.
If you aren't using Views, start with that.
If you're trying to build a slider, use Views Rotator.
If you want something that's ready to be dropped in, use Enterprise Rotator. (basically Views Rotator and a Banner Slide content type built out already).
You can offset the images by using the pager options to either show a certain number, and offset the number of images queued up.
Or if you want to just use specific images, use Nodequeue. Make a simple queue, add the nodes that contain the images you want, then in your View create a relationship (marked as required) to the nodequeue.

Show an image on just one dynamically created pages using an 'if statement'

We have several pages generated using PHP on our website with the following titles (for example):
http://www.mysite.com/project/category/1
http://www.mysite.com/project/category/2
http://www.mysite.com/project/category/3
Each one is created dynamically with the same page layout with each showing a different database result depending on the predefined conditions.
I would like an image to be displayed at the top of the page for just one of the results, let's say for http://www.mysite.com/project/category/2 - how can I go about this?
The relevant code on our page is this:
$category=mysql_fetch_array(mysql_query("select * from project_category where project_category_id='".$project_category_id."'"));?>
If we go down the if statement route can you show an example of how to display an example image by modifying the above code to get me started?
I would probably make it a property (can be a as simple yes/no) in the database, and use the existing db-result to determine if the category has to display a page. Although this might seem overkill - I'd definitely pick this dynamic solution over a if ($categoryId == 2) { } solution any day. Keeps it dynamic and your code clean and generic.
In the end I opted for an if statement (as found here http://www.tizag.com/phpT/if.php).
The original code above was modified in the following way:
$category=mysql_fetch_array(mysql_query("select * from project_category where project_category_id='".$project_category_id."'"));
if ( $project_category_id == "2" ) {
echo '<img src="http://www.mywebsite.com/image.jpg" width="675" height="75" border="0" />';
}?>

PHP Selecting HTML from the DB and apply changes

With a normal SELECT in the mySQL DB I select the HTML content of the final page.
I am building a way to create pages on the fly ( as any CMS does ) and I am saving the HTML code for each page inside a specific table fo my DB.
The problem is I want the content to be multi-language. But I do not know how to SELECT html code from the DB and before printing it, working on it with PHP.
I know it's hard to understand, this is why I have an example :)
$q='SELECT * FROM pages WHERE Page="'.$page.'"';
$r=mysql_query($q) or die(mysql_error());
while($row = mysql_fetch_array($r)) {
$content = $row['Content'];
}
return $content;
}
The Content of the variable $content, will be something like:
<div id="hello">{$foo}</div>
where {$foo} is in fact a PHP variable and must be run by the PHP code before printing the final HTML.
How can I do that? Also please consider I will have many {$foo} variables into the content, so I must find a way to replace all of them and to make the PHP working on them before printing the final HTML code.
Can you help me? :)

Categories