Reducing amount of images shown out database PHP MySQL - php

Lets start with;
echo $query_row['winkels'];
This will echo;
<td style="margin-left:3px;"><img src="logo/15.png"/></td> <td style="margin-left:3px;"><img src="logo/11.png"/></td>
Out of my MySql Database, but on the page it will echo one image. If I put more in like example;
<td style="margin-left:3px;"><img src="logo/15.png"/></td> <td style="margin-left:3px;"><img src="logo/11.png"/></td> <td style="margin-left:3px;"><img src="logo/15.png"/></td> <td style="margin-left:3px;"><img src="logo/11.png"/></td>
It will echo 2 images.
When I have more than 20 images shown I want to reduce it to 5 images.
How can I do that?
For example;
$winkels_inject = $query_row['winkels'];
$sub_winkels = substr($winkels_inject, 0, 191);
echo $sub_winkels;
This is perfect when trying to reduce text, but that is what it does when I use it. It reduces the image links and removes html so the images will not be shown. So no image will be shown at all.
How to fix this?
Regards,
F4LLCON

It seems you have a design problem, the only thing you would need to store in a DB, is the number and then every number in a different row.
Anyway, a quick and very dirty solution:
$string_with_breaks = str_replace('td> <td', 'td>__break_here__<td', $query_row['winkels']);
$img_array = explode('__break_here__', $string_with_breaks);
// loop through array and only echo the first 5 elements
$count = 0;
foreach($img_array as $store)
{
echo $store;
$count++;
if ($count > 4)
{
break;
}
}
Miss (or add...) a space between the td tags and it will not work anymore...

Related

Fixed table height with 4 cells inside (no scroll bars) & assigning text to each cell

I need to generate a table (HTML/CSS) with 4 cells inside (think 4 boxes one on top of another).
I have two questions-
How can i fix the width and height of each box (they may all have different heights) without no scroll bars if text overflows. If text does overflow, the cell height (& width) must stay same size and hide any overflowing text.
Using PHP (No frameworks) I populate these 4 cells with 4 different strings in a database. How can I assign
the text to each one of the 4 cells? e.g. $string[1] does into cell#1, $string[2] goes into cell#2 etc.
Thankyou.
Here is HTML/PHP that generates the table (in a calandar)
<table width="700" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#000000"> '
<tr>'
<td><table width="100%" border="0" cellpadding="0" cellspacing="1">'
for ($i = 1; $i <= $days_in_month; $i++) {'
echo "<td width=\"100\" height=\"100\" class=\"$class\">\n";
echo "<div align=\"right\"><span class=\"daynumber\">$i</span></div>\n";'
// keep count of cells across row, and if=7 end the row and start again '
}'
etc. (My calendar code works well, unlike my futile attempts to format my code here)
I store "events" in a mySql db, and when i=date in db, I output the text. The complex bit is trying to put 4 different event "data" into their corresponding boxes (see Q2 above).
to fix the width and height and to stop overflowing you can use css class.
.something{
width: 200px;
height : 100px;
overflow : hidden;
}
and also you can limit the text length using php substr() function
$limitedText = substr($yourstring, 0, 100);
to do this you have to use a foreach or while loop
foreach($queryResult as $rs){
echo "$rs['stringOne']";
echo "$rs['stringTwo']";
echo "$rs['stringThree']";
echo "$rs['stringFour']";
}

Extracting text using preg_match

I am trying to extract a piece of text from an HTML using PHP command preg_match.
Ive successfully parsed the HTML into a variable, but now I got stuck with extracting the right piece of information - probably because I am a bit confused by the syntax of preg_match.
So basically, here is a piece of the HTML I am interested in:
...<tr >
<td >Metuje</td>
<td ><a href="./detail_stanice/307158.html" >Maršov nad Metují</a></td>
<td >A</td>
<td >90</td>
<td >120</td>
<td >150</td>
<td >cm</td>
<td >04.08. 14:20</td>
<td >31</td>
<td >0.53</td>
<td ><img src="./img/ldown.png" width="15" /></td>
</tr>...
What I need is to find this particular row in the table (which contains couple of other rows), so basically I need to search for the name "Maršov nad Metují" in the second cell and then, extract the values of the subsequent cells on that row into a string, in other words in this particular case I would like to have a string with values A, 90, 120, etc. until the end of the row.
On the website there are then other rows with the exact same format just with different values, so I would then use the same syntax to extract values for rows with different names in the second cell.
I have tried it myself, but I was not able to get the right output.
I tried something like this, but this does not solve the problem, I know I have to somehow implement the cell TD commands, but unfortunately I wasnt able to get it right in this particular case.:
preg_match("/Maršov nad Metují(.*?)\<\/tr/", $html, $results);
Any help is very much appreciated.
Thanks
Try this :
<?php
$info = '<tr ><td >Metuje</td><td ><a href="./detail_stanice/307158.html" >Maršov nad Metují</a></td><td >A</td><td >90</td><td >120</td><td >150</td><td >cm</td><td >04.08. 14:20</td><td >31</td><td >0.53</td><td ><img src="./img/ldown.png" width="15" /></td></tr>';
preg_match('/<a href="(.*)" >(.*)</Ui',$info,$result);
print_r($result[2]);// Maršov nad Metují
preg_match_all("/<td.*?>(.+?)<\/td>/is", $html, $matches);
$result = $matches[1];
array_shift($result);
array_shift($result);
print implode(', ', $result);

PHP table that allows users to input height and width [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Hello. I have an assignment 9.2 to do and have no idea where to start.
I have made the index.php which allows the user to input the width and height of the table.
Here is the code for that:
<html>
<head>
<title>Assignment 9.2</title>
</head>
<body bgcolor="black" text="white">
<form method="post" action="table.php"
<strong>Please select an integer for the width:</strong>
<input type="text" name="width" size="10">
<br>
<strong>Please select an integer for the height:
<input type="text" name="width" size="10">
<input type="submit" value="Submit">
</form>
</body>
</html>
I do not know where to start on making the table. I do not expect to have this
done for me.. but to simply explain where to start and the php codes I need to use.
Again.. this is for 9.2 which is shown in the picture attached.
Okay, so this is a fairly simple thing to do if you understand how html tables are laid out in code...
HTML table format
The first step is to understand how tables are laid out in html...
3x2 table
For a 3 columned / 2 rowed table the code would look something like this...
Pseudo
opening table tag
opening row tag
cell tag
cell tag
cell tag
closing row tag
opening row tag
cell tag
cell tag
cell tag
closing row tag
closing table tag
HTML
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>
I don't know how much you know about html but hopefully that should make sense if not there are a wide variety of resources available that can explain the tags used etc...
PHP Code
At this point we're going to skip the input of width and height (as you already have the code for that) and assume that you've captured them as follows:
$in_width = $_POST['width'];
$in_height = $_POST['height'];
So now we know what the width and height of the table is going to be we can look at how to put them together:
If you look back at the example table code above you'll see that tables are rendered one row at a time in the format:
First row first column
First row second column
First row third column
Second row first column
...and so on...
This might be more clear by looking at the numbers in your screen grab above.
Given that we know how a html table is structured we can determine that we can use two loops* (the second one embedded in the first) to produce the desired output.
(The rows will be the outer loop and the columns will be in the inner loop - see example html code above)
Pseudo code
start table
for each row {
start row
for each column {
print cell
}
end row
}
end table
*You can use any loop you're comfortable with (i.e. for or loop) they do the same thing
If you want to do this yourself, I suggest that you stop reading here. Spoiler follows...
Spoiler
Two while loops
$in_height = $_POST['height']; // Get table height
$in_width = $_POST['width']; // Get table width
$counter = 1; // Start the counter at 1
$temp_width = 0; // Set temporary variable for width(second loop)
echo "<table>"; // Print opening tag for table
while($in_height > 0){
echo "<tr>"; // Print opening tag for row
$temp_width = $in_width; // Set temp_width to width of table
while($temp_width > 0){
echo "<td>$counter</td>"; // Print cell with counter value
++$counter; // Increment counter
--$temp_width; // Decrement temp_width
}
echo "</tr>"; // Print closing tag for row
--$in_height; // Decrement height
}
echo "</table>"; // Print closing tag for table
Two for loops
Just for demonstration purposes...
$in_height = $_POST['height'];
$in_width = $_POST['width'];
$counter = 1;
$temp_width = 0;
$temp_height = 0;
echo "<table>";
for($temp_height = 0; $temp_height < $in_height; $temp_height++) {
echo "<tr>";
for($temp_width = 0; $temp_width < $in_width; $temp_width++){
echo "<td>$counter</td>";
++$counter;
}
echo "</tr>";
}
echo "</table>";
You could also mix and match loops...

Generate an HTML table with dynamic row Height

I need to print an html table with row heights sets dynamically based on some values from the database using PHP. seems that html 5 doesn't support inline height and with tags and using css instead.
My requirement is to generate an html file and then convert it into pdf using DOM pdf.
Please guide me how to set these parameters dynamically inline or using css or whether a library already available for the same purpose.
I Googled a lot, but unable find any results matching my requirement.
Also am attaching final output format
(In answer column i printed some values which is the height required for each row)
Thanks in advance
You can use inline styles:
<tr style="height: 300px;"></tr>
I am not sure if you can effectively set the height of a <tr> tag, so you might have to set the height of each <td> in the row individually. Give it a try.
Furthermore, I am not sure how you have your array of rows and columns structured, but this might shed some light on how to do it.
<?php
$array=array(array(50,'r1c1','r1c2'),array(50,'r2c1','r2c2'));
echo '<table>';
foreach($array as $row)
{
echo '<tr style="height: '.$row[0].'px;">';
echo '</tr>';
for($i=1;$i<count($row);++$1)
{
echo '<td>'.$row[$i].'</td>';
}
}
echo '</table>';
?>
If you still need help, post the exact array you wish to turn into a <table> and I will do my best to assist.
If I understand this right, your table rows can be different sizes from each other, but for each row there is a rule in database, that sets row's height, no matter what height the content of the row, right? Then you can use something like this:
<html>
<head>
<style>
<?php foreach($yourRows as $key => $row) { ?>
#row<?=$key;?>{
height: <?=$row['height']; ?>px;
}
<?php } ?>
</style>
</head>
<body>
<table>
<?php foreach($yourRows as $key => $row) { ?>
<tr id="row<?=$key; ?>">
...
</tr>
<?php } ?>
</table>
</body>
In the style tag you can replace "#row<?=$key;?>" with "#row<?=$key;?> td"
Updated
Anyway, if you want to use the inline styling, you can make it happen like that:
<html>
<body>
<table>
<?php foreach($yourRows as $row) { ?>
<tr style="height:<?=$row['height']; ?>px">
Or you can apply height to td instead of the tr...
</tr>
<?php } ?>
</table>
</body>
If you think that jQuery might work here is a suggestion. I'm not sure it works with DOMPDF but as we're dynamically creating CSS it should be fine once the DOM has loaded.
If you know exactly the heights of each row - then select them using jQuery using eq.
$(document).ready(function() {
$('table tr').eq(1).css({'height':'250'});
$('table tr').eq(3).css({'height':'450'});
});
Here is the fiddle.
That way you don't have to modify the output but you have to make the assumption the content isn't going to be higher than your fixed height.
If you need this to be more dynamic then you'll need to either associate identifiers to your rows, like a class or something like that. Or alternatively, if you have a pattern in your content is to create a regular expression that scans your content and identifies it that way - then you can apply CSS rules to these rows once matched using jQuery.
EDIT
OK so I may have slightly misunderstood if you have the height value stored in the database. It also looks as though you've determined already that you're unable to use inline styles.
Here is my next suggestion.
You're building the table from a loop so it probably looks something like this.
foreach($rows as $row) {
echo '<tr data-height="'.$row['height'].'"><td>...</td></tr>;
}
if you add data-height="'.$row['height'].'" then you have a value that we can get using jQuery's data like so.
$(document).ready(function() {
$('table tr').each(function() {
var height = $(this).data('height');
$(this).css({ 'height': height });
});
});
Here is an example fiddle with static data-height values. Let me know how you get on.

Got a bunch of images, need to put them together to form a big picture (like a puzzle)

I got a bunch of images (225 in total). Example of their names:
4n27e.png
4n28e.png
4n29e.png
4n30e.png
5n12e.png
5n25e.png
5n26e.png
5n27e.png
5n28e.png
I need to form one big picture out of all these images. For example first line of images would be 4n27e (2nd image 4n28e, 3rd image 4n29e and so on). Second line of images would start from 5n12e (2nd image 5n25e and so on). What methods do i need to search for to solve this?
Note: i need to do this only with php and maybe some javascript.
Test case for logic, you can replace the echo with an img tag, and /or use div.
<?php
$images=array();
$images[]="4n27e.png";
$images[]="4n28e.png";
$images[]="4n29e.png";
$images[]="4n30e.png";
$images[]="5n12e.png";
$images[]="5n25e.png";
$images[]="5n26e.png";
$images[]="5n27e.png";
$images[]="5n28e.png";
echo "<table border='1'>";
$oldIndex=0;
$row=1;
foreach($images as $image)
{
if(substr($image,0,1)!=$oldIndex)
{
if($row>1){echo "</tr>";}
echo "<tr>";
$oldIndex=substr($image,0,1);
$row++;
}
echo "<td>$image</td>";
}
echo "</table>";
?>

Categories