Okay guys this is going to be one long question so please bear with me.
I want to create an accordion with contents in it:
How I want the image to look like
How it looks right now
I have a web application, where I upload various types(blog, homepage) of images every week. The first week is round 1 and the image can be labelled something like 1.x.x where 1 is the number corresponding to the week. Likewise for 2.x.x, 3.x.x etc.
I insert these images to a server using php and log the entry into a database, with information like project_name, file_name, url, version etc.
What I want to do is, automatically generate multiple accordion tabs one below the other. First tab should be the round corresponding to the latest week of uploading. Below that the week before and like wise till it reaches round 1(See images attached).
Now I am able to pull data and display files from ALL the rounds in a single round. What I want to do is separate rounds, automatically based on version number.=>> 1.x.x files in Round 1, 2.x.x in round 2 etc
Following is the code that I have written. The commented part is what I have been follow and and do. I am a total new comer to PHP and would appreciate it if anyone could help me with this. Thanks!
$project_name_download_form = $_GET['project_name'];
$file_name_download_form = $_GET['file_name'];
$version_download_form = $_GET['version'];
$download_data = "SELECT *
FROM projects, files
WHERE project_id = projects.id
AND project_name = '$project_name_download_form'
AND file_name = '$file_name_download_form'
AND version = '$version_download_form'";
mysqli_select_db($conn, $GLOBALS['database']);
$return_data = mysqli_query($conn, $download_data);
if (!$return_data) {
die('Could not get what you wanted: ' . mysqli_error($conn));
}
while ($row = mysqli_fetch_array($return_data)) {
$url = $row['url'];
$project_folder = $row['project_folder'];
//$id = 18;
$file_name = $row['file_name'];
$version = $row['version'];
$details = $row['details'];
$file = $row['file'];
$new = basename($row['file']); // GET FILE NAME ONLY, GET RID OF PATH.
// $design->url = $row[file_name]
// $design->version = $row[version]
// $designs.push ($design)
}
mysqli_free_result($return_data);
// $lastRound = 0;
// for each $design in $designs
// {
// $round = $design.version.split('.')[0];
// if $round <> $lastRound {
?>
<div class="col-sm-2" id = "accordianSet">
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne"><span>
</span>Explore</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
<?php
}
?>
<?php
// <HTML FOR LINK >
}
echo "<table class = 'table'>";
echo "<tr>";
echo " <th>File Name</th>
<th>File Link</th>";
echo "</tr>";
What you want to do is pull the results for each week and loop through but each time you loop you want to print variables in html like follows:
<?php
$i=0
while ($row = mysqli_fetch_array($return_data)) {
$url = $row['url'];
$project_folder = $row['project_folder'];
//$id = 18;
$file_name = $row['file_name'];
$version = $row['version'];
$details = $row['details'];
$file = $row['file'];
$new = basename($row['file']);
$i++
//1st Round
echo' <table>
<caption> Round $i</caption>
<tr>
<th>File Name</th>
<td>$file_name</td>
</tr>
<tr>....
</tr>
</table>';
}//while loop close tag
Note that you can format the data anyway you want but you have to spit out the html as you loop through the mysql results. Hope this helps.
Related
I am trying to create a small website to sell items on and learn php. I have a index.php which links to a chairs.php page and then from there to a viewItem.php page depending on which item they choose.
I have in my database 4 items. On the chairs.php a while loop runs to display on the page all items in that database.
I would like to use the id of the item to link to the next page but I think the while loop is always going to assign to the variable that I am using the very last entry in that while loop.
Can someone point me in the direction of how I would resolve this? I would like the user to select a Chair 1 on my Chairs.php page with ID 1 in my db to link to the corresponding page. Currently all links on the chairs.php would link to the last item in my while loop which is then assigned to the $_SESSION['output_id'] = $output_id variable.
Thank You
<?php
$query = "SELECT * FROM `chairs_table";
$queryChairs = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($queryChairs)) {
$output_id = $row["ID"];
$output_img = $row["Image"];
$output_name = $row["Name"];
$output_desc = $row["Desc"];
$output_price = $row["Price"];
$_SESSION['output_id'] = $output_id;
$_SESSION['output_img'] = $output_img;
$_SESSION['output_name'] = $output_name;
$_SESSION['output_desc'] = $output_desc;
$_SESSION['output_price'] = $output_price;
?>
<div class="itemContainer" onclick="location.href = 'viewItem.php?id=<?php echo "{$output_id}" ?>'" style="cursor:pointer;" >
<div> <img src = "img/<?php echo "{output_img}" ?>"> </div>
<div class="itemTextTitle"><?php echo"{$output_name}" ?></div>
<div class="itemTextDesc"><?php echo"{$output_desc}" ?></div>
<div class="itemTextPrice">cop <?php echo"{$output_price}"?></div>
</div>
<?php
}
?>
</div>
I am fairly new to PHP and mySQL and have small issue I am busy trying to create a 4 column layout with using echo and currently I have managed to achieve this with with a mySQL database but the last column requires and additional style in the div class. With my Limited knowledge I thought of trying a count using one of the solutions I found on the forum, but I think I may be using it wrong. my idea is to echo the database array, then do a count and echo the same content block but the with additional css element in div class. Your help is greatly appreciated. if this question has been asked before please accept my apologies I am just under a lot of pressure to get it resolved.
SEE CURRENT PHP Code Below
<?php
$conn = mysqli_connect('localhost', 'root', 'password')
or die ( mysql_error());
$rs = mysqli_select_db($conn, 'database name' ) or die
( mysql_error());
$query = "SELECT * FROM database name";
$rs = mysqli_query( $conn, $query ) or die
( mysql_error());
while( $row = mysqli_fetch_array($rs, MYSQLI_BOTH))
{
$id = $row['id'];
$image = $row['image'];
$product = $row['product'];
$code = $row['code'];
$material = $row['material'];
$sizes = $row['sizes'];
echo "
<div class='percent-one-fourth'>
<div class='catalogue_content'>
<img src='bridal_gowns/$image.jpg' alt='Bridal Gown Catalogue - $product'>
<h3>$product</h3>
<div class='content'>
<p id='content'><span style='font-weight:bold; color:#969;'>Code:</span> $code<br>
<span style='font-weight:bold; color:#969;'>Material:</span> $material<br>
<span style='font-weight:bold; color:#969;'>Sizes Available:</span> $sizes
<br>
<br>
</p>
<a href='mailto:lizette#bridalalchemy.co.za; raymond#bridalalchemy.co.za?subject=Enquiry from Web / Product: $product, Code: $code;'><img src='enquire.jpg'></a>
</div>
</div>
</div>
";
}
$i = 0;
while( $row = mysqli_fetch_array($rs, MYSQLI_BOTH)){
if($i++ % 4 == 0) {
echo "
<div class='percent-one-fourth column-last'>
<div class='catalogue_content'>
<img src='bridal_gowns/$image.jpg' alt='Bridal Gown Catalogue - $product'>
<h3>$product</h3>
<div class='content'>
<p id='content'><span style='font-weight:bold; color:#969;'>Code:</span> $code<br>
<span style='font-weight:bold; color:#969;'>Material:</span> $material<br>
<span style='font-weight:bold; color:#969;'>Sizes Available:</span> $sizes
<br>
<br>
</p>
<a href='mailto:lizette#bridalalchemy.co.za; raymond#bridalalchemy.co.za?subject=Enquiry from Web / Product: $product, Code: $code;'><img src='enquire.jpg'></a>
</div>
</div>
</div>
";
}
}
mysqli_close($conn);
?>
As you're new to PHP, it's really important that you understand why your code isn't working properly. So, let's step over it and describe it in brief, simple English:
Connect to MySQL
Run a database query which has multiple rows as its response
Whilst there are more rows to go..
Output every row
Whilst there are more rows to go.. [!] Always false - we already used them all up
Output every 4th result [!] Vars like $image also aren't defined here
So, because of the way how mysqli_fetch_array works, you can hopefully see that we need to loop using it only once. This is also great for you, because it fits with the DRY (don't repeat yourself) principle.
Edit: You were also mixing MySQL API's; mysql_error is very different from mysqli_error.
So instead, during our one loop, we'll check if we're on the 4th row, and react differently. That results in something like this:
<?php
// 1. Connect to MySQL
// (this should be in a separate file and included so you don't repeat it)
$conn = mysqli_connect('localhost', 'root', 'password')
or die ( mysqli_error()); // <-- *mysqli*
$rs = mysqli_select_db($conn, 'database name' ) or die
( mysqli_error());
// 2. Run the query which has multiple rows as its response:
$query = "SELECT * FROM database name";
$rs = mysqli_query( $conn, $query ) or die
( mysqli_error());
// 3. Whilst there are more rows to go..
$i=0;
while( $row = mysqli_fetch_array($rs, MYSQLI_BOTH))
{
// Get easier references to various fields:
$id = $row['id'];
$image = $row['image'];
$product = $row['product'];
$code = $row['code'];
$material = $row['material'];
$sizes = $row['sizes'];
// Start outputting this row:
echo "<div class='percent-one-fourth";
// The important part! We check here in this single loop:
if($i++ % 4 == 0){
// Every 4th row - output that extra class now!
echo " column-last";
}
// Output everything else:
echo "'>
<div class='catalogue_content'>
<img src='bridal_gowns/$image.jpg' alt='Bridal Gown Catalogue - $product'>
<h3>$product</h3>
<div class='content'>
<p id='content'><span style='font-weight:bold; color:#969;'>Code:</span> $code<br>
<span style='font-weight:bold; color:#969;'>Material:</span> $material<br>
<span style='font-weight:bold; color:#969;'>Sizes Available:</span> $sizes
<br>
<br>
</p>
<a href='mailto:lizette#bridalalchemy.co.za; raymond#bridalalchemy.co.za?subject=Enquiry from Web / Product: $product, Code: $code;'><img src='enquire.jpg'></a>
</div>
</div>
</div>
";
}
// Tidy up:
mysqli_close($conn);
?>
Your code is echoing only for the 4th column. For columns 1, 2, and 3, you need to add an else clause.
Optional: Besides instead of echoing you could close the php tag (?>) and write the html directly.
...
$i = 0;
while( $row = mysqli_fetch_array($rs, MYSQLI_BOTH)){
if($i++ % 4 == 0) {
echo "html for the 4th column";
}else{
echo "html for column 1, 2, and 3";
}
}
I am using php to publish news articles from my mysql database. The articles have images placed within their text. The images are stored as variables, each its own path. The problem is I don't always use the full complement of images for each article, and the way I've written it, a broken image displays.
In short, how can I keep broken images from showing up if say '$image_ii' or '$image_iii' does not exist for a specific ID.
The way I have it written out is as follows:
<?php
include ('connect.php');
$friendly_url=$_GET['friendly_url'];
$query = "SELECT * FROM entries WHERE friendly_url='$friendly_url'";
$entries = mysql_query($query);
while($row = mysql_fetch_array($entries, MYSQL_ASSOC))
{
$title = $row['title'];
$author = $row['author'];
$pub_date = $row['pub_date'];
$content_i = $row['content_i'];
$content_ii = $row['content_ii'];
$content_iii = $row['content_iii'];
$content_iv = $row['content_iv'];
$image_i = $row['image_i'];
$image_ii = $row['image_ii'];
$image_iii = $row['image_iii'];
$image_caption_i = $row['image_caption_i'];
$image_caption_ii = $row['image_caption_ii'];
$image_caption_iii = $row['image_caption_iii'];
$pre_link = $row['pre_link'];
$post_link = $row ['post-link'];
$id = $row['id'];
$friendly_url = $row['friendly_url'];
$american_date = date("F d, Y", strtotime($pub_date));
$friendly_content_i = nl2br($content_i);
$friendly_content_ii = nl2br($content_ii);
$friendly_content_iii = nl2br($content_iii);
$friendly_content_iv = nl2br($content_iv);
echo "
<div id='post'>
<div id='post-title'>$title</div>
<div id='post-content'
$friendly_content_i
<div id='media'> <img src='../$image_i' class='scale-image'> </div>
<div id='media-caption'> $image_caption_i </div>
$friendly_content_ii
<div id='media'> <img src='../$image_ii' class='scale-image'> </div>
<div id='media-caption'> $image_caption_ii </div>
$friendly_content_iii
<div id='media'> <img src='../$image_iii' class='scale-image'> </div>
<div id='media-caption'> $image_caption_iii </div>
$friendly_content_iv
</div>
<div id='post-footer'> by $author <br> $american_date </div>
</div>
</div> ";
} mysql_close($connection); ?>
Apologies if this is terrible, it's my first dynamic website.
Try using the following snippet
if (file_exists("../".$image_iii)) {
echo "<img src='../$image_iii' class='scale-image'>";
} else {
echo "<img src='../default-image' class='scale-image'>";
}
I ended up finding a solution that worked using:
if (!empty($image_iii)) { echo "<img src='../$image_iii' class='scale-image'>"; }
else { echo "";}
That said, clearly you kind folks are right about me needing to swap over to mysqli...found this website as a good resource for migrating code to mysqli. Thanks to everyone who responded, everyones comments were helpful.
You already told us the solution to your problem:
if say '$image_ii' or '$image_iii' does not exist for a specific ID.
Use if file_exists() to check that your image exists, and don't output it, if it doesn't.
Furthermore I see you're using mysql_query - This is deprecated and very unsafe. You should use mysqli or PDO instead
How how would I just grab the first few words of a variable such as $data for example. I'm trying to create an article preview section and that variable is an article. I'm trying to display of course only a preview of it.
<?php
$sql = "SELECT author, data, postdate, Title, previewimg FROM articlepreview ORDER BY RAND() LIMIT 1";
$query = mysqli_query($db_conx,$sql);
$prev = "";
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$a = $row["author"];
$data = $row["data"];
$postdate = $row["postdate"];
$title = $row["Title"];
$previewimg = $row["previewimg"];
$prev .= '
<div class="wrapPreviewArticle">
<div style="headerPreview"><h4>'.$title.'</h4></div>
<div class="wrapPreviewRow">
<div style="imagePreview"><img src="'.$previewimg.'" height="20%" width="20%" alt="'.$title.'" /></div>
<div clas="wordPreview">'.$data.'</div>
</div>
</div>
';
}
?>
Once I've got this I would spit out the variable
<body>
<div><?php echo $prev; ?></div> <!--with only the desired portion.-->
</body>
Any help is greatly appreciated.
You could use SUBSTRING_INDEX on your query:
"SELECT author, SUBSTRING_INDEX(data, ' ', 5) data, ... FROM articlepreview ..."
This will extract only the first 5 words from the field data.
You can grab for example the first 200 characters and display only this as a preview
$pos=strpos($data, ' ', 200);
$preview=substr($data,0,$pos );
This will also make sure you split it at the space not in the middle of the word
You could show the first X characters (X here is 20):
<div class="wordPreview">'.substr($data, 0, 20).'</div>
I have this PHP code :
$query = "SELECT * FROM news WHERE news_active = 1 AND news_type = 1 ORDER BY id DESC";
$q2 = "SELECT * FROM st_photos WHERE id = 4 LIMIT 1";
$r2 = mysql_query($q2);
$row22 = mysql_fetch_array($r2);
$news_set = mysql_query($query);
$news_set2 = mysql_query($query);
if (mysql_num_rows($news_set) != 0) {
$r = mysql_fetch_array($news_set);
echo "<div id=\"d_coll\">
<div id=\"top_text\">$row22[img]</div>
<div id=\"d_image\"><img id=\"larg_p2\" src=\"photos/$r[news_image]\" width=\"320\" height=\"250\" border=\"0\"></div>
<div style=\"width:300px\"><div id=\"n_text2\">$r[news_part_en]</div>
</div>
</div>";
}
if (mysql_num_rows($news_set2) != 0) {
while ($news = mysql_fetch_array($news_set2)) {
echo "<div id=\"n_col\">
<div id=\"n_tittle\">$news[news_tittle_en] <img src=\"images/bu3.png\" border=\"0\" align=\"middle\"></div>
<div id=\"im\"><img onMouseOver=\"MM_swapImage('larg_p2','','photos/$news[news_image]','imgs[$news[id]]','','photos/$news[news_image]',1);up2('$news[news_part_en]')\" onMouseOut=\"MM_swapImgRestore()\" name=\"imgs[$news[id]]\" id=\"imgs[$news[id]]\" src=\"photos/$news[news_image]\" width=\"50\" height=\"50\"></div>
<div dir=\"ltr\" id=\"n_div\">$news[news_part_en] <div class=\"mo\">MORE</div></div>
</div>";
}
echo "<div align=\"right\" class=\"arr\"><img src=\"images/prev.png\"> <img src=\"images/next.png\"></div>";
}
There are 2 images at the end of the code (prev & next), I want to use them to do pagination but I don't want to view any numbers, only these 2 images.
How I can do that?
I think we can do that by using JQuery library, but I don't know how to use it.
Thanks in advance.
you can use one of many plugins, for example here .
you must just remoove thе numbers. you can, i believe;)
or you can write the script by yourself.
i'll give you only an idea. let's assume you have 30 rows( from DB).put them into <div> tags, and increase the id of div. the display proparty of first <div> you must set to '', and all others to none. and then, onclick on your buttons, you just change display proparty of div elements...