Format only first cell in each line?
-
Hi,
I want to skip the first line and then format the first cell in each line after that with a link like this:
:Heading here…:
[link- https://mydomain/data1a%5Ddata1a%5B/link%5D data1b data1c
[link- https://mydomain/data2a%5Ddata2a%5B/link%5D data2b data2cIs this possible and where do I customize this? What files should I work with?
-
Hi Asle,
you can do that with the css selector nth-child.
Have a look here how it works:
https://css-tricks.com/how-nth-child-works/Greetings
Maybe I was not clear with my question. This is not a CSS issue. I want the first cell in each row to be a URL. And skip the header. I changed the code in /wp-content/plugins/wp-excel-cms/public/templates/ to the following that works.
The excel sheet is displayed as a table. The first cell is a link to a product like https://domain.com/id_number (this id_number is the cells value)
<table class="wp_excel_cms_table wp_excel_cms_table_<?php echo $name; ?>"> <?php $i = 0; $end=0; // to count and get the first cell ?> <?php foreach($data as $entry): ?> <?php if($i==0){$count = count($entry);} ?> <tr> <?php if($i==0): ?> <?php foreach ($entry as $cell): ?> <th><?php echo $cell; ?></th> <?php endforeach; ?> <?php else: foreach ($entry as $cell): ?> <td><?php if($end==0) { // then this is the first cell echo '<a href="https://domain.com/'.$cell.'" target="_blank">' .$cell. "</a>" ; $end=1; } else { echo $cell;} ?></td> <?php endforeach; ?> <?php endif; ?> </tr> <?php $end = 0; // reset $end ?> <?php $i++; ?> <?php endforeach; ?> </table>
Maybe i am a bit late, but i have practically the same problem. The difference is that i need instead of first cell in each row where are simbols “N” insert a little picture. Good example of what i seek would be https://kinas.info/?boxoffice. Any suggestions?
Is this cell always the second one? If so make sure the counter is = 1 and then check if the cell contains the data to replace or trigger. You must first replace the symbol “N” in your code to excel. Then you can look for the symbol. E.g. your cell contains “##” where the “N” symbol is now.
<table class="wp_excel_cms_table wp_excel_cms_table_<?php echo $name; ?>"> <?php $i = 0; $end=0; // to count and get the first cell foreach($data as $entry): ?> if($i==0){$count = count($entry);} ?> <tr> <?php if($i==0): ?> foreach ($entry as $cell): ?> <th><?php echo $cell; ?></th> <?php endforeach; else: foreach ($entry as $cell): ?> <td><?php if($end==1) && strstr($cell,'##')) { // then this is the second cell // AND it contains string "##" echo '<img src="https://domain.com/m_image.gif" alt="" />'; $end=2; } else { echo $cell;} ?></td> <?php endforeach; endif; ?> </tr> <?php $end = 0; // reset $end $i++; // increase counter endforeach; ?> </table>
Thank you for your reply asle, but you forgot to insert some brackets. Except that everything works like a charm.
Sorry, I took it out of my head and did not test it. Saw the bracket missing now. Glad you got the idea anyway. Wish there was a way to use different filters on different documents. But then I guess I would have to write my own plugin if the author does not add this function.
I have 1 more question. What should i do now if i need to compare numbers in first and second cell, so if number in first cell is bigger than in second insert THAT.jpg picture, if they are equal insert THAT_2.jpg and if in second cell is bigger than in first insert THAT_3.jpg? Also in second cell can be not only number, but “-” or “N”. I thought that i needed to simply change if option with:
<?php if($end==0){$getchar = substr($cell,0,1);} ?>
<?php if($end==1){$getchar2 = substr($cell,0,1);} ?>
<?php if(($end==1) && ($getchar < $getchar2)){
echo "'<img src="https://www.clker.com/.../1.jpg" alt="" />'; $end=2; }
else if(($end==1) && ($getchar > $getchar2)){
echo "'<img src="https://www.clker.com/.../2.jpg" alt="" />'; $end=2; }
else if(($end==1) && ($getchar == $getchar2)){
echo "'<img src="https://www.clker.com/.../3.jpg" alt="" />'; $end=2; }
else {
echo $cell;} ?></td>
but is that right?
So you are inserting this image at the end of the row? This looks ok then. If you want to insert the image from a logic from the first and second cell at the start of the row you would have to add all the cells to variables and print them at the end of the row-loop. But I guess you are adding the image after the cell values you are comparing?
If the logic doesn’t work it may be you have to convert the string to integer like
$getchar = (int)substr($cell,0,1);
Sorry, i did mistake. I want to insert those images at the second cell from the beginning instead of number that i want to compare with other number in the first cell.
5 8 ….So it would first of all compare them. Then if 8 is bigger than 5(true) -> insert specific image to that case in second cell where is right now number 8. If for example there would be 19 and 11, instead of 11 would appear different image.
As i understand i need not only read those numbers, but convert them to integer like as you said.
I already tried 2 more methods, but they start reading only from letters.1)<?php if($end==0){$int = filter_var($cell, FILTER_SANITIZE_NUMBER_INT); $int = intval("$int"); print("$int"); $end=1;} ?> <?php if($end==1){$int2 = filter_var($cell, FILTER_SANITIZE_NUMBER_INT); $int2 = intval("$int2"); print("$int2"); $end=2;} ?> <?php if(($end==1) && ($int < $int2)){... ?> 2)<?php if($end==0){$int = (int)$cell[0];} if($end==1){$int2 = (int)$cell[0];} if($end==1 && ($int < $int2)){echo ' <img src="https://www.clker.com/cliparts/b/l/n/Q/E/5/check-mark-13x13.svg" alt="" />'; $end=2; }
It’s just a thought but maybe i am failing at inserting those images.
To get the numbers out of a string no matter where the numbers are you could use something like this:
$number = preg_replace(“/[^0-9]/”, ”, $cell); // ditch anything that is not a number
So this would both give number 33
– “33sometext”
– “sometext33”
- The topic ‘Format only first cell in each line?’ is closed to new replies.