Generate a Table of contents every time a var changes - php

I tried to find any solution or question and I didn't find anything.
I'm working on a medical directory, is a table with 3 columns and 3 td's in every row when the html is done I need to convert it to a PDF version.
So far everything is fine.
But later, I was asked for a change.
To insert an index of contents every time the city changed.
But the structure must be the same in all cases, 3 cols with 3 tds for row
This is the structure of my table:
$totals=count($bloque[$i]);
for($i=0;$i<$totales;$i++){
if($i%3 == 0) {$impression[]="<tr valign='top' >".$bloque3[$i];}
if($i%3 == 1) {$impression[]=$bloque3[$i];}
if($i%3 == 2) {$impression[]=$bloque[$i]."</tr>";}
}
After that I'm doing the comparison if the city is different then structure of the index.
if($thecity != $rows['city']){
$index='<tr><th><span class="index" style="font-size:11px;"><small><b>'.$rows2['city '].'<br><br>Index of contents</b></small></span></th></tr>';
}
else{$index='';}
Next var is the structure of every td.
$bloque3[]='<span>'.$index.'</span><td width="30%"><p>'.$rows2['provider'].'</p>'.'<p>'.$rows2['service'].'</span>'.'<span>'.$rows2['benefits'].'<br />'.$rows2['city'].'</span></td>';
.
Every index must be in a new row, but I'm including the index in the same array bloque because otherwise is not working.
The problem is every time $index exist, is inserting a row inside a row, which is not too bad in the browser, because browsers are intelligent - most of them ;)-
But in pdf version the tds looks awful, one above another, and index above another td.

Related

Echo repeated variables loop php

you see, I have these variables that are set within a While, I'm using MySQL also, What I want to do is to sort of "Group" those variables into one, and then the rest of the variables that are not repeated, i want to assign them "Display:none" So , look at this image:
Now, as you can see there is two duplicate dates, but the values are different, so in this case (Take in mind that I only want to group these "Recibos" but dont worry about it) you can see in the first row it has a different date, and has no repeated dates, while the last two of "Recibos" is sort of duplicated
Basically what I want to do is to group the two last "Recibos" (The duplicate dates), only one of the same date has to appear, and the rest of them, are gonna be shown when the TR is hover
Though , I only want a simple version of what I want, it can be some sort of testing, as you can see in this image, i've been trying for weeks and still didnt get anything, I hope you can help me! thanks in advance
I have made this code, which is the one in the image above:
Inside the while:
$acumulado-=$monto;
if(($fila>=$mostrar_de_aca_en_adelante && $detalles=='minimo') || ($detalles=='maximos')){
$conteo++;
$string_fecha[$conteo] = $fecha_operacion;
$string_fecha_tmp[$conteo] = $fecha_operacion;
$recibo_id_cliente[$conteo]=$id_cliente;
$recibo_monto[$conteo]=$monto;
$recibo_saldo_total[$conteo]=$saldo_total;
$recibo_detalle_movimiento[$conteo]=$datos_detalles['detalle_movimiento'];
$recibo_recibo[$conteo]=$recibo[0];
$recibo_verificado[$conteo]=$datos_detalles['verificado'];
$recibo_acumulado[$conteo]=$acumulado;
$label = $monto;
}
Outside the while:
echo "<br><br><br>";
for($i = 1; $i <= $conteo; $i++){
if($string_fecha[$i] == $string_fecha_tmp[($i + 1)]){
$label += $recibo_monto[$i];
$string_imprime_tmp .= "<br>[Repetido]".$string_fecha[$i]."(".$label.")";
}
}
echo $string_imprime_tmp;
var_dump($string_fecha);
By the way: I know this can be done in MySQL, but it can only be done in php i'm limited to php.

Invoking (looping through) a list of MySQL rows in PHP gives unexpected result

Two snippets of PHP code:
The first one works fine, giving values from different rows sequentially.
while ($row = mysql_fetch_assoc($rst)) {
...
$link1 = $row['audio1'];
if ($link1) {
$link1 = $link1;
}
...
}
The second one outputs the value from the first row and applies it to every subsequent row.
while ($row = mysql_fetch_assoc($rst)) {
...
if ($row['audio1']) {
$link1 = $row['audio1'];
}
...
}
I.e. where the first code sample generates
Audio1.mp3
Audio2.mp3
Audio3.mp3
the second one would generate
Audio1.mp3
Audio1.mp3
Audio1.mp3
The question is why those two similar implementations differ in output.
Another test case with ...s removed (clean loop code) yields a different picture: the first example only outputs one line (the first one), the second example's output is the same which is repeating lines.
MySQL table that is being used has the following structure:
One table,
INT (id), CHAR, SMALLINT, TINYTEXT, TEXT, TEXT, TINYTEXT (audio1). All of the rows are fully populated except for the last one (audio1) which is mostly empty and has non-NULL values somewhere in the middle (if we sort by id field).
Replacing the 'audio1' with another column name yields correct results (for that field) in both cases.
Another addition is that I do indeed place a SELECT statement initially in mysql_query that filters out rows. Depending on the number of rows being returned the picture for the second example may be slightly different, like 3 non-repeating values followed by a repetition of the third value until the end.
I fixed it. Long story short - it was really obvious: between loop cycles $link1 variable didn't change in case if wasn't true. What I needed to do was to put $link1 = ""; or unset ($link); right after while and in that case if I ever encountered an empty value for $row['audio1'] it would stay truly empty instead of reusing the old value for every new row.

SQL in PHP: Checking if number of rows is divisible by a certain number

I have to do some php work for a student group I am part at in school. Clients make use of our provided services, and every ten requests (which are made online) they make of our service, they are required to do a check-in.
When we receive the request, we get all the info (id, special requests, notes, etc) from our database. I've added the form that indicates if a check-in is required no problem...no issues with the front end. It's just a matter of the backend that I'm unsure of due to my lack of experience in php and mysql.
Here is an example:
echo "<td>Half Size</td>";
echo "<td>". ($value["halfsize"] == 1 ? "Yes" : "No") . "<br/>";
echo "</td>";
echo "</tr><tr>";
If clients want half size for the request, they fill it out...when we receive it, we see whether that want a full size of half size.
Here is what I want to do with pseudocode, but since I'm lacking in SQL knowledge, I'm not sure what to do.
echo "<td>Check-in Required</td>";
**//Check count of rows (any table)
//If divisible by 10 (%10), return "Yes", else "No"**
echo "</td>";
echo "</tr><tr>";
What I need to figure out is how to check the number of rows, if that can be divisible by 10 then a check-in is required.
Hopefully that was clear enough and you understand what I'm asking, because reading it back it still sounds a bit hard to follow. I'll be happy to clarify if you have any questions. Thank you!
I'm not sure I completely get what you're doing, but I think what you need is the modulo operator (%).
if(count($rows) % 10 == 0){ // divisible by 10
}
else{ //not divisible by 10
}
PHP supports modulo which gets the remainder.
$divisor = 10;
$rowCount = 4; // put your method here that returns the number of rows
$result = $rowCount % $divisor == 0 ? "Yes" : "No";
PHP Modulo
You can alter the database table for clients, add one more column to store the count value.
Add: a column, say, counter --> integer to clients table.
Change your code to do:
Every time client makes a request, query select * from clients where id =
take out counter value and check if (counter%10 == 0).
if(counter%10 == 0)
{
request to check in;
}
accept request;
then update clients counter column (increment by 1)
Updating table will remember requests from previous sessions too. But if you want to check number of requests in only current session, then you just have to add a counter variable in your PHP code and check for %10 before every request.
This was just a pseudo-code. If its tenth request, you request check in, and then accept request and increment clients counter.
else you just accept request and increment counter.
(Sorry for writing just pseudo code, instead of actual code. But that is one way to do it. I may have misunderstood the question, but if this solves your problem, I'm happy.)

PHP - Echo amount of pictures based on mysql_num_rows

I want to code a script that will echo an amount of images based on the mysql_num_rows(); function.
Like, I'm storing 3 in a row. Then I'll create:
$images = mysql_num_rows($count);
Then I would like it to echo 3 images.
Is this possible?
If you don't get the question or want me to rewrite it, please just tell me.
Then i think, all you need to do is fetch the rating (i.e. a number) from the database and just use that number directly to display the number of stars. Coz mysql_rows_count would give you the total number of records in a result set which is returned as a result of a select query on multiple columns, but in your case you only need to access one column which would contain the rating and another column which would probably contain user id or some sort of key to associate the ratings with. The result set can be fetched as an associative array which can then be used by indices to fetch the right column. Have a look at this: PHP-mysql_fetch_array() .
EDIT: just to sum up (not the actual syntax) :
$array = mysql_fetch_array(<your select query fetching only the needed columns>);
$images = $array[0] // assuming the rating number is at the 0th index
//image display code, do your stuff
So here is what I did:
I had the numbers from 1-5 in my database in a row called 'rating'. Now when I picked this out of the database I stuffed the number (1-5) into a variable called '$starRating'.
for($i=1;$i<=$starRating;$i++){
echo "<span class='star'><span class='starText'>$i</span></span>";
}
Then I created a class called star and starText:
.star{
width:18px;
height:18px;
background:url('../images/design/smallImg/star.png') no-repeat;
text-align:center;
float:right;
margin-top:2px;
}
/* star content */
.starText{
display:none;
}
.starText is made to hide the text that would've been from the for() loop.
* I don't know if you can avoid the text another way around *
This works for me perfectly.
I wrote this to help people searching for an easy solution to this.

Populating with records on Html Table

A php custom function returns record set from a mysql query.
I need to populate a two column table having equal quantity or balanced between two.
I know we can split by number of columns the row count then use a loop to populate each td accordingly.
Any suggestions or most practical way to display record sets onto tables organized according to their sizes?
Not using templates at this time.
Basically need to control the TD tag from been echo or not and filled.
Sample Code:
$col_num = 2; //Number of colums to parse data onto.
$count_r = mysql_num_rows($result)/$col_num;
echo '<table style="width:400px;border:3px solid green;"><tr><td>';
$ui=1;
while($row = mysql_fetch_array($result))
{
if($ui<$count_r)
{
echo '<input disabled type="checkbox" value="$row[id]"'; $this->ischecked($uid,$row[id]); echo "/>".$row['name'].'<br>'."\r\n";
}
$ui++;
}
//FIRST COLUMN SET
echo '</td>';
echo '<td>';
//SECOND COLUMN SET
echo '</td></tr></table>';
If you don't mind about the order of your records you could simply do (assuming you've got your results in an array called $result) the following:
...
echo "<table>";
for($i=0;$i<count($result);$i+=2):
echo "<tr>";
echo "<td>{$result[$i]['field']}</td>";
echo "<td>{$result[$i+1]['field']}</td>";
echo "</tr>";
endfor;
echo "</table>";
I don't know if it's the best way of doing this but it's the way I'd do it in your case. Keep in mind that I'm not checking if there are any actual results, we are assuming that it's the case. To check if $results is set you could use an if statement checking it's count number for example.
Note
Replace for($i=0;$i<count($result);$i+=2): with for($i=0;$i<count($result);$i+=2){ and endfor; with } for a traditionally formatted code.
I'd recommend using an unordered list -- <ul>, and apply the CSS float attribute to its list items (<li>). That way, you won't have to worry about counting the number of records.
Just output each record in a <li>, and set them all to float: left;.
Then constrain the height of the <ul> so it will be short enough to force the extra <li>s to float to a new column.
As #Steve pointed out, you'll also need to set the <li> width to be constant so the columns will be consistent. And make the width of your <ul> more than twice and less than three times as big as the <li> width.
A basic way to do that would be to call mysql_num_rows() on your query. Multiply the result by the height of each <li>, then divide by two, and that will be the height of your <ul>. That will make each column of equal height.
You could also do it with <div>s (one <div> for each item, all wrapped in a single global <div>).
But using a list is the preferred way of displaying a list of information in HTML (unless the information is tabular in nature and requires multiple columns per field, which yours isn't and doesn't).
If you need more detailed help, there's a great Opera Web Standards article on this.

Categories